added iOS source code
[wl-app.git] / iOS / Pods / RealmSwift / RealmSwift / ObjectSchema.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  This class represents Realm model object schemas.
24
25  When using Realm, `ObjectSchema` instances allow performing migrations and introspecting the database's schema.
26
27  Object schemas map to tables in the core database.
28  */
29 public struct ObjectSchema: CustomStringConvertible {
30
31     // MARK: Properties
32
33     internal let rlmObjectSchema: RLMObjectSchema
34
35     /**
36      An array of `Property` instances representing the managed properties of a class described by the schema.
37
38      - see: `Property`
39      */
40     public var properties: [Property] {
41         return rlmObjectSchema.properties.map { Property($0) }
42     }
43
44     /// The name of the class the schema describes.
45     public var className: String { return rlmObjectSchema.className }
46
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)
51         }
52         return nil
53     }
54
55     /// A human-readable description of the properties contained in the object schema.
56     public var description: String { return rlmObjectSchema.description }
57
58     // MARK: Initializers
59
60     internal init(_ rlmObjectSchema: RLMObjectSchema) {
61         self.rlmObjectSchema = rlmObjectSchema
62     }
63
64     // MARK: Property Retrieval
65
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)
70         }
71         return nil
72     }
73 }
74
75 // MARK: Equatable
76
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)
81     }
82 }