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 `Schema` instances represent collections of model object schemas managed by a Realm.
25 When using Realm, `Schema` instances allow performing migrations and introspecting the database's schema.
27 Schemas map to collections of tables in the core database.
29 public struct Schema: CustomStringConvertible {
33 internal let rlmSchema: RLMSchema
36 An array of `ObjectSchema`s for all object types in the Realm.
38 This property is intended to be used during migrations for dynamic introspection.
40 public var objectSchema: [ObjectSchema] {
41 return rlmSchema.objectSchema.map(ObjectSchema.init)
44 /// A human-readable description of the object schemas contained within.
45 public var description: String { return rlmSchema.description }
49 internal init(_ rlmSchema: RLMSchema) {
50 self.rlmSchema = rlmSchema
53 // MARK: ObjectSchema Retrieval
55 /// Looks up and returns an `ObjectSchema` for the given class name in the Realm, if it exists.
56 public subscript(className: String) -> ObjectSchema? {
57 if let rlmObjectSchema = rlmSchema.schema(forClassName: className) {
58 return ObjectSchema(rlmObjectSchema)
66 extension Schema: Equatable {
67 /// Returns whether the two schemas are equal.
68 public static func == (lhs: Schema, rhs: Schema) -> Bool {
69 return lhs.rlmSchema.isEqual(to: rhs.rlmSchema)