1 ////////////////////////////////////////////////////////////////////////////
3 // Copyright 2015 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 ////////////////////////////////////////////////////////////////////////////
22 `ObjectiveCSupport` is a class providing methods for Swift/Objective-C interoperability.
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.
27 Use this to provide public APIs that support both platforms.
31 public final class ObjectiveCSupport {
33 /// Convert a `Results` to a `RLMResults`.
34 public static func convert<T>(object: Results<T>) -> RLMResults<AnyObject> {
35 return object.rlmResults
38 /// Convert a `RLMResults` to a `Results`.
39 public static func convert(object: RLMResults<AnyObject>) -> Results<Object> {
40 return Results(object)
43 /// Convert a `List` to a `RLMArray`.
44 public static func convert<T>(object: List<T>) -> RLMArray<AnyObject> {
45 return object._rlmArray
48 /// Convert a `RLMArray` to a `List`.
49 public static func convert(object: RLMArray<AnyObject>) -> List<Object> {
50 return List(rlmArray: object)
53 /// Convert a `LinkingObjects` to a `RLMResults`.
54 public static func convert<T>(object: LinkingObjects<T>) -> RLMResults<AnyObject> {
55 return object.rlmResults
58 /// Convert a `RLMLinkingObjects` to a `Results`.
59 public static func convert(object: RLMLinkingObjects<RLMObject>) -> Results<Object> {
60 return Results(object)
63 /// Convert a `Realm` to a `RLMRealm`.
64 public static func convert(object: Realm) -> RLMRealm {
65 return object.rlmRealm
68 /// Convert a `RLMRealm` to a `Realm`.
69 public static func convert(object: RLMRealm) -> Realm {
73 /// Convert a `Migration` to a `RLMMigration`.
74 public static func convert(object: Migration) -> RLMMigration {
75 return object.rlmMigration
78 /// Convert a `RLMMigration` to a `Migration`.
79 public static func convert(object: RLMMigration) -> Migration {
80 return Migration(object)
83 /// Convert a `ObjectSchema` to a `RLMObjectSchema`.
84 public static func convert(object: ObjectSchema) -> RLMObjectSchema {
85 return object.rlmObjectSchema
88 /// Convert a `RLMObjectSchema` to a `ObjectSchema`.
89 public static func convert(object: RLMObjectSchema) -> ObjectSchema {
90 return ObjectSchema(object)
93 /// Convert a `Property` to a `RLMProperty`.
94 public static func convert(object: Property) -> RLMProperty {
95 return object.rlmProperty
98 /// Convert a `RLMProperty` to a `Property`.
99 public static func convert(object: RLMProperty) -> Property {
100 return Property(object)
103 /// Convert a `Realm.Configuration` to a `RLMRealmConfiguration`.
104 public static func convert(object: Realm.Configuration) -> RLMRealmConfiguration {
105 return object.rlmConfiguration
108 /// Convert a `RLMRealmConfiguration` to a `Realm.Configuration`.
109 public static func convert(object: RLMRealmConfiguration) -> Realm.Configuration {
110 return .fromRLMRealmConfiguration(object)
113 /// Convert a `Schema` to a `RLMSchema`.
114 public static func convert(object: Schema) -> RLMSchema {
115 return object.rlmSchema
118 /// Convert a `RLMSchema` to a `Schema`.
119 public static func convert(object: RLMSchema) -> Schema {
120 return Schema(object)
123 /// Convert a `SortDescriptor` to a `RLMSortDescriptor`.
124 public static func convert(object: SortDescriptor) -> RLMSortDescriptor {
125 return object.rlmSortDescriptorValue
128 /// Convert a `RLMSortDescriptor` to a `SortDescriptor`.
129 public static func convert(object: RLMSortDescriptor) -> SortDescriptor {
130 return SortDescriptor(keyPath: object.keyPath, ascending: object.ascending)
133 /// Convert a `SyncCredentials` to a `RLMSyncCredentials`.
134 public static func convert(object: SyncCredentials) -> RLMSyncCredentials {
135 return RLMSyncCredentials(object)
138 /// Convert a `RLMSyncCredentials` to a `SyncCredentials`.
139 public static func convert(object: RLMSyncCredentials) -> SyncCredentials {
140 return SyncCredentials(object)
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))
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))
157 /// Convert a `SyncConfiguration` to a `RLMSyncConfiguration`.
158 public static func convert(object: SyncConfiguration) -> RLMSyncConfiguration {
159 return object.asConfig()
162 /// Convert a `RLMSyncConfiguration` to a `SyncConfiguration`.
163 public static func convert(object: RLMSyncConfiguration) -> SyncConfiguration {
164 return SyncConfiguration(config: object)