1 ////////////////////////////////////////////////////////////////////////////
3 // Copyright 2014 Realm Inc.
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 ////////////////////////////////////////////////////////////////////////////
23 This class represents Realm model object schemas.
25 When using Realm, `ObjectSchema` instances allow performing migrations and introspecting the database's schema.
27 Object schemas map to tables in the core database.
29 public struct ObjectSchema: CustomStringConvertible {
33 internal let rlmObjectSchema: RLMObjectSchema
36 An array of `Property` instances representing the managed properties of a class described by the schema.
40 public var properties: [Property] {
41 return rlmObjectSchema.properties.map { Property($0) }
44 /// The name of the class the schema describes.
45 public var className: String { return rlmObjectSchema.className }
47 /// The property which serves as the primary key for the class the schema describes, if any.
48 public var primaryKeyProperty: Property? {
49 if let rlmProperty = rlmObjectSchema.primaryKeyProperty {
50 return Property(rlmProperty)
55 /// A human-readable description of the properties contained in the object schema.
56 public var description: String { return rlmObjectSchema.description }
60 internal init(_ rlmObjectSchema: RLMObjectSchema) {
61 self.rlmObjectSchema = rlmObjectSchema
64 // MARK: Property Retrieval
66 /// Returns the property with the given name, if it exists.
67 public subscript(propertyName: String) -> Property? {
68 if let rlmProperty = rlmObjectSchema[propertyName] {
69 return Property(rlmProperty)
77 extension ObjectSchema: Equatable {
78 /// Returns whether the two object schemas are equal.
79 public static func == (lhs: ObjectSchema, rhs: ObjectSchema) -> Bool {
80 return lhs.rlmObjectSchema.isEqual(to: rhs.rlmObjectSchema)