-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
ObjectiveCSupport.swift
235 lines (198 loc) · 8.49 KB
/
ObjectiveCSupport.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2015 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
import Realm
/**
`ObjectiveCSupport` is a class providing methods for Swift/Objective-C interoperability.
With `ObjectiveCSupport` you can either retrieve the internal ObjC representations of the Realm objects,
or wrap ObjC Realm objects with their Swift equivalents.
Use this to provide public APIs that support both platforms.
:nodoc:
**/
@frozen public struct ObjectiveCSupport {
/// Convert a `Results` to a `RLMResults`.
public static func convert<T>(object: Results<T>) -> RLMResults<AnyObject> {
return object.collection as! RLMResults<AnyObject>
}
/// Convert a `RLMResults` to a `Results`.
public static func convert(object: RLMResults<AnyObject>) -> Results<Object> {
return Results(object)
}
/// Convert a `List` to a `RLMArray`.
public static func convert<T>(object: List<T>) -> RLMArray<AnyObject> {
return object.rlmArray
}
/// Convert a `MutableSet` to a `RLMSet`.
public static func convert<T>(object: MutableSet<T>) -> RLMSet<AnyObject> {
return object.rlmSet
}
/// Convert a `RLMArray` to a `List`.
public static func convert(object: RLMArray<AnyObject>) -> List<Object> {
return List(collection: object)
}
/// Convert a `RLMSet` to a `MutableSet`.
public static func convert(object: RLMSet<AnyObject>) -> MutableSet<Object> {
return MutableSet(collection: object)
}
/// Convert a `Map` to a `RLMDictionary`.
public static func convert<Key, Value>(object: Map<Key, Value>) -> RLMDictionary<AnyObject, AnyObject> {
return object.rlmDictionary
}
/// Convert a `RLMDictionary` to a `Map`.
public static func convert<Key>(object: RLMDictionary<AnyObject, AnyObject>) -> Map<Key, Object> {
return Map(objc: object)
}
/// Convert a `LinkingObjects` to a `RLMResults`.
public static func convert<T>(object: LinkingObjects<T>) -> RLMResults<AnyObject> {
return object.collection as! RLMResults<AnyObject>
}
/// Convert a `RLMLinkingObjects` to a `Results`.
public static func convert(object: RLMLinkingObjects<RLMObject>) -> Results<Object> {
return Results(object)
}
/// Convert a `Realm` to a `RLMRealm`.
public static func convert(object: Realm) -> RLMRealm {
return object.rlmRealm
}
/// Convert a `RLMRealm` to a `Realm`.
public static func convert(object: RLMRealm) -> Realm {
return Realm(object)
}
/// Convert a `Migration` to a `RLMMigration`.
@available(*, deprecated, message: "This function is now redundant")
public static func convert(object: Migration) -> RLMMigration {
return object
}
/// Convert a `ObjectSchema` to a `RLMObjectSchema`.
public static func convert(object: ObjectSchema) -> RLMObjectSchema {
return object.rlmObjectSchema
}
/// Convert a `RLMObjectSchema` to a `ObjectSchema`.
public static func convert(object: RLMObjectSchema) -> ObjectSchema {
return ObjectSchema(object)
}
/// Convert a `Property` to a `RLMProperty`.
public static func convert(object: Property) -> RLMProperty {
return object.rlmProperty
}
/// Convert a `RLMProperty` to a `Property`.
public static func convert(object: RLMProperty) -> Property {
return Property(object)
}
/// Convert a `Realm.Configuration` to a `RLMRealmConfiguration`.
public static func convert(object: Realm.Configuration) -> RLMRealmConfiguration {
return object.rlmConfiguration
}
/// Convert a `RLMRealmConfiguration` to a `Realm.Configuration`.
public static func convert(object: RLMRealmConfiguration) -> Realm.Configuration {
return .fromRLMRealmConfiguration(object)
}
/// Convert a `Schema` to a `RLMSchema`.
public static func convert(object: Schema) -> RLMSchema {
return object.rlmSchema
}
/// Convert a `RLMSchema` to a `Schema`.
public static func convert(object: RLMSchema) -> Schema {
return Schema(object)
}
/// Convert a `SortDescriptor` to a `RLMSortDescriptor`.
public static func convert(object: SortDescriptor) -> RLMSortDescriptor {
return object.rlmSortDescriptorValue
}
/// Convert a `RLMSortDescriptor` to a `SortDescriptor`.
public static func convert(object: RLMSortDescriptor) -> SortDescriptor {
return SortDescriptor(keyPath: object.keyPath, ascending: object.ascending)
}
/// Convert a `RLMShouldCompactOnLaunchBlock` to a Realm Swift compact block.
@preconcurrency
public static func convert(object: @escaping RLMShouldCompactOnLaunchBlock) -> @Sendable (Int, Int) -> Bool {
return { totalBytes, usedBytes in
return object(UInt(totalBytes), UInt(usedBytes))
}
}
/// Convert a Realm Swift compact block to a `RLMShouldCompactOnLaunchBlock`.
@preconcurrency
public static func convert(object: @Sendable @escaping (Int, Int) -> Bool) -> RLMShouldCompactOnLaunchBlock {
return { totalBytes, usedBytes in
return object(Int(totalBytes), Int(usedBytes))
}
}
/// Convert a RealmSwift before block to an RLMClientResetBeforeBlock
@preconcurrency
public static func convert(object: (@Sendable (Realm) -> Void)?) -> RLMClientResetBeforeBlock? {
guard let object = object else {
return nil
}
return { localRealm in
return object(Realm(localRealm))
}
}
/// Convert an RLMClientResetBeforeBlock to a RealmSwift before block
@preconcurrency
public static func convert(object: RLMClientResetBeforeBlock?) -> (@Sendable (Realm) -> Void)? {
guard let object = object else {
return nil
}
return { localRealm in
return object(localRealm.rlmRealm)
}
}
/// Convert a RealmSwift after block to an RLMClientResetAfterBlock
@preconcurrency
public static func convert(object: (@Sendable (Realm, Realm) -> Void)?) -> RLMClientResetAfterBlock? {
guard let object = object else {
return nil
}
return { localRealm, remoteRealm in
return object(Realm(localRealm), Realm(remoteRealm))
}
}
/// Convert an RLMClientResetAfterBlock to a RealmSwift after block
@preconcurrency
public static func convert(object: RLMClientResetAfterBlock?) -> (@Sendable (Realm, Realm) -> Void)? {
guard let object = object else {
return nil
}
return { localRealm, remoteRealm in
return object(localRealm.rlmRealm, remoteRealm.rlmRealm)
}
}
/// Converts a swift block receiving a `SyncSubscriptionSet`to a RLMFlexibleSyncInitialSubscriptionsBlock receiving a `RLMSyncSubscriptionSet`.
@preconcurrency
public static func convert(block: @escaping @Sendable (SyncSubscriptionSet) -> Void) -> RLMFlexibleSyncInitialSubscriptionsBlock {
return { subscriptionSet in
return block(SyncSubscriptionSet(subscriptionSet))
}
}
/// Converts a block receiving a `RLMSyncSubscriptionSet`to a swift block receiving a `SyncSubscriptionSet`.
@preconcurrency
public static func convert(block: RLMFlexibleSyncInitialSubscriptionsBlock?) -> (@Sendable (SyncSubscriptionSet) -> Void)? {
guard let block = block else {
return nil
}
return { subscriptionSet in
return block(subscriptionSet.rlmSyncSubscriptionSet)
}
}
/// Converts a block receiving a `RLMSyncSubscriptionSet`to a swift block receiving a `SyncSubscriptionSet`.
@preconcurrency
public static func convert(block: @escaping RLMFlexibleSyncInitialSubscriptionsBlock) -> @Sendable (SyncSubscriptionSet) -> Void {
return { subscriptionSet in
return block(subscriptionSet.rlmSyncSubscriptionSet)
}
}
}