added iOS source code
[wl-app.git] / iOS / Pods / RealmSwift / RealmSwift / ObjectiveCSupport.swift
1 ////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2015 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 Realm
20
21 /**
22  `ObjectiveCSupport` is a class providing methods for Swift/Objective-C interoperability.
23
24  With `ObjectiveCSupport` you can either retrieve the internal ObjC representations of the Realm objects,
25  or wrap ObjC Realm objects with their Swift equivalents.
26
27  Use this to provide public APIs that support both platforms.
28
29  :nodoc:
30  **/
31 public final class ObjectiveCSupport {
32
33     /// Convert a `Results` to a `RLMResults`.
34     public static func convert<T>(object: Results<T>) -> RLMResults<AnyObject> {
35         return object.rlmResults
36     }
37
38     /// Convert a `RLMResults` to a `Results`.
39     public static func convert(object: RLMResults<AnyObject>) -> Results<Object> {
40         return Results(object)
41     }
42
43     /// Convert a `List` to a `RLMArray`.
44     public static func convert<T>(object: List<T>) -> RLMArray<AnyObject> {
45         return object._rlmArray
46     }
47
48     /// Convert a `RLMArray` to a `List`.
49     public static func convert(object: RLMArray<AnyObject>) -> List<Object> {
50         return List(rlmArray: object)
51     }
52
53     /// Convert a `LinkingObjects` to a `RLMResults`.
54     public static func convert<T>(object: LinkingObjects<T>) -> RLMResults<AnyObject> {
55         return object.rlmResults
56     }
57
58     /// Convert a `RLMLinkingObjects` to a `Results`.
59     public static func convert(object: RLMLinkingObjects<RLMObject>) -> Results<Object> {
60         return Results(object)
61     }
62
63     /// Convert a `Realm` to a `RLMRealm`.
64     public static func convert(object: Realm) -> RLMRealm {
65         return object.rlmRealm
66     }
67
68     /// Convert a `RLMRealm` to a `Realm`.
69     public static func convert(object: RLMRealm) -> Realm {
70         return Realm(object)
71     }
72
73     /// Convert a `Migration` to a `RLMMigration`.
74     public static func convert(object: Migration) -> RLMMigration {
75         return object.rlmMigration
76     }
77
78     /// Convert a `RLMMigration` to a `Migration`.
79     public static func convert(object: RLMMigration) -> Migration {
80         return Migration(object)
81     }
82
83     /// Convert a `ObjectSchema` to a `RLMObjectSchema`.
84     public static func convert(object: ObjectSchema) -> RLMObjectSchema {
85         return object.rlmObjectSchema
86     }
87
88     /// Convert a `RLMObjectSchema` to a `ObjectSchema`.
89     public static func convert(object: RLMObjectSchema) -> ObjectSchema {
90         return ObjectSchema(object)
91     }
92
93     /// Convert a `Property` to a `RLMProperty`.
94     public static func convert(object: Property) -> RLMProperty {
95         return object.rlmProperty
96     }
97
98     /// Convert a `RLMProperty` to a `Property`.
99     public static func convert(object: RLMProperty) -> Property {
100         return Property(object)
101     }
102
103     /// Convert a `Realm.Configuration` to a `RLMRealmConfiguration`.
104     public static func convert(object: Realm.Configuration) -> RLMRealmConfiguration {
105         return object.rlmConfiguration
106     }
107
108     /// Convert a `RLMRealmConfiguration` to a `Realm.Configuration`.
109     public static func convert(object: RLMRealmConfiguration) -> Realm.Configuration {
110         return .fromRLMRealmConfiguration(object)
111     }
112
113     /// Convert a `Schema` to a `RLMSchema`.
114     public static func convert(object: Schema) -> RLMSchema {
115         return object.rlmSchema
116     }
117
118     /// Convert a `RLMSchema` to a `Schema`.
119     public static func convert(object: RLMSchema) -> Schema {
120         return Schema(object)
121     }
122
123     /// Convert a `SortDescriptor` to a `RLMSortDescriptor`.
124     public static func convert(object: SortDescriptor) -> RLMSortDescriptor {
125         return object.rlmSortDescriptorValue
126     }
127
128     /// Convert a `RLMSortDescriptor` to a `SortDescriptor`.
129     public static func convert(object: RLMSortDescriptor) -> SortDescriptor {
130         return SortDescriptor(keyPath: object.keyPath, ascending: object.ascending)
131     }
132
133     /// Convert a `SyncCredentials` to a `RLMSyncCredentials`.
134     public static func convert(object: SyncCredentials) -> RLMSyncCredentials {
135         return RLMSyncCredentials(object)
136     }
137
138     /// Convert a `RLMSyncCredentials` to a `SyncCredentials`.
139     public static func convert(object: RLMSyncCredentials) -> SyncCredentials {
140         return SyncCredentials(object)
141     }
142
143     /// Convert a `RLMShouldCompactOnLaunchBlock` to a Realm Swift compact block.
144     public static func convert(object: @escaping RLMShouldCompactOnLaunchBlock) -> (Int, Int) -> Bool {
145         return { totalBytes, usedBytes in
146             return object(UInt(totalBytes), UInt(usedBytes))
147         }
148     }
149
150     /// Convert a Realm Swift compact block to a `RLMShouldCompactOnLaunchBlock`.
151     public static func convert(object: @escaping (Int, Int) -> Bool) -> RLMShouldCompactOnLaunchBlock {
152         return { totalBytes, usedBytes in
153             return object(Int(totalBytes), Int(usedBytes))
154         }
155     }
156
157     /// Convert a `SyncConfiguration` to a `RLMSyncConfiguration`.
158     public static func convert(object: SyncConfiguration) -> RLMSyncConfiguration {
159         return object.asConfig()
160     }
161
162     /// Convert a `RLMSyncConfiguration` to a `SyncConfiguration`.
163     public static func convert(object: RLMSyncConfiguration) -> SyncConfiguration {
164         return SyncConfiguration(config: object)
165     }
166 }