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 #if BUILDING_REALM_SWIFT_TESTS
26 // MARK: Internal Helpers
28 // Swift 3.1 provides fixits for some of our uses of unsafeBitCast
29 // to use unsafeDowncast instead, but the bitcast is required.
30 internal func noWarnUnsafeBitCast<T, U>(_ x: T, to type: U.Type) -> U {
31 return unsafeBitCast(x, to: type)
34 /// Given a list of `Any`-typed varargs, unwrap any optionals and
35 /// replace them with the underlying value or NSNull.
36 internal func unwrapOptionals(in varargs: [Any]) -> [Any] {
37 return varargs.map { arg in
39 if let someArg = arg as Any? {
44 if let optionalArg = arg as? RealmAnyOptionalUnboxingWorkaround {
45 return optionalArg.rlm_unwrappedValue()
53 // FIXME: Kill this when we drop Xcode 8.0 support.
56 private protocol RealmAnyOptionalUnboxingWorkaround {
57 func rlm_unwrappedValue() -> Any
60 extension Optional: RealmAnyOptionalUnboxingWorkaround {
61 func rlm_unwrappedValue() -> Any {
63 case .none: return NSNull()
64 case let .some(underlying): return underlying
70 internal func notFoundToNil(index: UInt) -> Int? {
71 if index == UInt(NSNotFound) {
77 internal func throwRealmException(_ message: String, userInfo: [AnyHashable: Any]? = nil) {
78 NSException(name: NSExceptionName(rawValue: RLMExceptionName), reason: message, userInfo: userInfo).raise()
81 internal func throwForNegativeIndex(_ int: Int, parameterName: String = "index") {
83 throwRealmException("Cannot pass a negative value for '\(parameterName)'.")
87 internal func gsub(pattern: String, template: String, string: String, error: NSErrorPointer = nil) -> String? {
88 let regex = try? NSRegularExpression(pattern: pattern, options: [])
89 return regex?.stringByReplacingMatches(in: string, options: [],
90 range: NSRange(location: 0, length: string.utf16.count),
91 withTemplate: template)
94 internal func cast<U, V>(_ value: U, to: V.Type) -> V {
95 if let v = value as? V {
98 return unsafeBitCast(value, to: to)
102 // Must *only* be used to call Realm Objective-C APIs that are exposed on `RLMObject`
103 // but actually operate on `RLMObjectBase`. Do not expose cast value to user.
104 internal func unsafeCastToRLMObject() -> RLMObject {
105 return unsafeBitCast(self, to: RLMObject.self)
109 // MARK: CustomObjectiveCBridgeable
111 internal func dynamicBridgeCast<T>(fromObjectiveC x: Any) -> T {
112 if T.self == DynamicObject.self {
113 return unsafeBitCast(x as AnyObject, to: T.self)
114 } else if let BridgeableType = T.self as? CustomObjectiveCBridgeable.Type {
115 return BridgeableType.bridging(objCValue: x) as! T
121 internal func dynamicBridgeCast<T>(fromSwift x: T) -> Any {
122 if let x = x as? CustomObjectiveCBridgeable {
129 // Used for conversion from Objective-C types to Swift types
130 internal protocol CustomObjectiveCBridgeable {
131 static func bridging(objCValue: Any) -> Self
132 var objCValue: Any { get }
135 // FIXME: needed with swift 3.2
136 // Double isn't though?
137 extension Float: CustomObjectiveCBridgeable {
138 static func bridging(objCValue: Any) -> Float {
139 return (objCValue as! NSNumber).floatValue
142 return NSNumber(value: self)
146 extension Int8: CustomObjectiveCBridgeable {
147 static func bridging(objCValue: Any) -> Int8 {
148 return (objCValue as! NSNumber).int8Value
151 return NSNumber(value: self)
154 extension Int16: CustomObjectiveCBridgeable {
155 static func bridging(objCValue: Any) -> Int16 {
156 return (objCValue as! NSNumber).int16Value
159 return NSNumber(value: self)
162 extension Int32: CustomObjectiveCBridgeable {
163 static func bridging(objCValue: Any) -> Int32 {
164 return (objCValue as! NSNumber).int32Value
167 return NSNumber(value: self)
170 extension Int64: CustomObjectiveCBridgeable {
171 static func bridging(objCValue: Any) -> Int64 {
172 return (objCValue as! NSNumber).int64Value
175 return NSNumber(value: self)
178 extension Optional: CustomObjectiveCBridgeable {
179 static func bridging(objCValue: Any) -> Optional {
180 if objCValue is NSNull {
183 return .some(dynamicBridgeCast(fromObjectiveC: objCValue))
187 if let value = self {
188 return dynamicBridgeCast(fromSwift: value)
195 // MARK: AssistedObjectiveCBridgeable
197 internal protocol AssistedObjectiveCBridgeable {
198 static func bridging(from objectiveCValue: Any, with metadata: Any?) -> Self
199 var bridged: (objectiveCValue: Any, metadata: Any?) { get }