added iOS source code
[wl-app.git] / iOS / Pods / RealmSwift / RealmSwift / Schema.swift
1 ////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2014 Realm Inc.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17 ////////////////////////////////////////////////////////////////////////////
18
19 import Foundation
20 import Realm
21
22 /**
23  `Schema` instances represent collections of model object schemas managed by a Realm.
24
25  When using Realm, `Schema` instances allow performing migrations and introspecting the database's schema.
26
27  Schemas map to collections of tables in the core database.
28  */
29 public struct Schema: CustomStringConvertible {
30
31     // MARK: Properties
32
33     internal let rlmSchema: RLMSchema
34
35     /**
36      An array of `ObjectSchema`s for all object types in the Realm.
37
38      This property is intended to be used during migrations for dynamic introspection.
39      */
40     public var objectSchema: [ObjectSchema] {
41         return rlmSchema.objectSchema.map(ObjectSchema.init)
42     }
43
44     /// A human-readable description of the object schemas contained within.
45     public var description: String { return rlmSchema.description }
46
47     // MARK: Initializers
48
49     internal init(_ rlmSchema: RLMSchema) {
50         self.rlmSchema = rlmSchema
51     }
52
53     // MARK: ObjectSchema Retrieval
54
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)
59         }
60         return nil
61     }
62 }
63
64 // MARK: Equatable
65
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)
70     }
71 }