123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- import Foundation
- import Realm
- #if BUILDING_REALM_SWIFT_TESTS
- import RealmSwift
- #endif
- internal func noWarnUnsafeBitCast<T, U>(_ x: T, to type: U.Type) -> U {
- return unsafeBitCast(x, to: type)
- }
- internal func unwrapOptionals(in varargs: [Any]) -> [Any] {
- return varargs.map { arg in
- if let someArg = arg as Any? {
- return someArg
- }
- return NSNull()
- }
- }
- internal func notFoundToNil(index: UInt) -> Int? {
- if index == UInt(NSNotFound) {
- return nil
- }
- return Int(index)
- }
- internal func throwRealmException(_ message: String, userInfo: [AnyHashable: Any]? = nil) -> Never {
- NSException(name: NSExceptionName(rawValue: RLMExceptionName), reason: message, userInfo: userInfo).raise()
- fatalError()
- }
- internal func throwForNegativeIndex(_ int: Int, parameterName: String = "index") {
- if int < 0 {
- throwRealmException("Cannot pass a negative value for '\(parameterName)'.")
- }
- }
- internal func gsub(pattern: String, template: String, string: String, error: NSErrorPointer = nil) -> String? {
- let regex = try? NSRegularExpression(pattern: pattern, options: [])
- return regex?.stringByReplacingMatches(in: string, options: [],
- range: NSRange(location: 0, length: string.utf16.count),
- withTemplate: template)
- }
- internal func cast<U, V>(_ value: U, to: V.Type) -> V {
- if let v = value as? V {
- return v
- }
- return unsafeBitCast(value, to: to)
- }
- extension Object {
-
-
- internal func unsafeCastToRLMObject() -> RLMObject {
- return unsafeBitCast(self, to: RLMObject.self)
- }
- }
- public func dynamicBridgeCast<T>(fromObjectiveC x: Any) -> T {
- if T.self == DynamicObject.self {
- return unsafeBitCast(x as AnyObject, to: T.self)
- } else if let bridgeableType = T.self as? CustomObjectiveCBridgeable.Type {
- return bridgeableType.bridging(objCValue: x) as! T
- } else if let bridgeableType = T.self as? RealmEnum.Type {
- return bridgeableType._rlmFromRawValue(x) as! T
- } else {
- return x as! T
- }
- }
- public func dynamicBridgeCast<T>(fromSwift x: T) -> Any {
- if let x = x as? CustomObjectiveCBridgeable {
- return x.objCValue
- } else if let bridgeableType = T.self as? RealmEnum.Type {
- return bridgeableType._rlmToRawValue(x)
- } else {
- return x
- }
- }
- internal protocol CustomObjectiveCBridgeable {
- static func bridging(objCValue: Any) -> Self
- var objCValue: Any { get }
- }
- extension Float: CustomObjectiveCBridgeable {
- internal static func bridging(objCValue: Any) -> Float {
- return (objCValue as! NSNumber).floatValue
- }
- internal var objCValue: Any {
- return NSNumber(value: self)
- }
- }
- extension Int8: CustomObjectiveCBridgeable {
- internal static func bridging(objCValue: Any) -> Int8 {
- return (objCValue as! NSNumber).int8Value
- }
- internal var objCValue: Any {
- return NSNumber(value: self)
- }
- }
- extension Int16: CustomObjectiveCBridgeable {
- internal static func bridging(objCValue: Any) -> Int16 {
- return (objCValue as! NSNumber).int16Value
- }
- internal var objCValue: Any {
- return NSNumber(value: self)
- }
- }
- extension Int32: CustomObjectiveCBridgeable {
- internal static func bridging(objCValue: Any) -> Int32 {
- return (objCValue as! NSNumber).int32Value
- }
- internal var objCValue: Any {
- return NSNumber(value: self)
- }
- }
- extension Int64: CustomObjectiveCBridgeable {
- internal static func bridging(objCValue: Any) -> Int64 {
- return (objCValue as! NSNumber).int64Value
- }
- internal var objCValue: Any {
- return NSNumber(value: self)
- }
- }
- extension Optional: CustomObjectiveCBridgeable {
- internal static func bridging(objCValue: Any) -> Optional {
- if objCValue is NSNull {
- return nil
- } else {
- return .some(dynamicBridgeCast(fromObjectiveC: objCValue))
- }
- }
- internal var objCValue: Any {
- if let value = self {
- return dynamicBridgeCast(fromSwift: value)
- } else {
- return NSNull()
- }
- }
- }
- internal protocol AssistedObjectiveCBridgeable {
- static func bridging(from objectiveCValue: Any, with metadata: Any?) -> Self
- var bridged: (objectiveCValue: Any, metadata: Any?) { get }
- }
|