Util.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. import Foundation
  19. import Realm
  20. #if BUILDING_REALM_SWIFT_TESTS
  21. import RealmSwift
  22. #endif
  23. // MARK: Internal Helpers
  24. // Swift 3.1 provides fixits for some of our uses of unsafeBitCast
  25. // to use unsafeDowncast instead, but the bitcast is required.
  26. internal func noWarnUnsafeBitCast<T, U>(_ x: T, to type: U.Type) -> U {
  27. return unsafeBitCast(x, to: type)
  28. }
  29. /// Given a list of `Any`-typed varargs, unwrap any optionals and
  30. /// replace them with the underlying value or NSNull.
  31. internal func unwrapOptionals(in varargs: [Any]) -> [Any] {
  32. return varargs.map { arg in
  33. if let someArg = arg as Any? {
  34. return someArg
  35. }
  36. return NSNull()
  37. }
  38. }
  39. internal func notFoundToNil(index: UInt) -> Int? {
  40. if index == UInt(NSNotFound) {
  41. return nil
  42. }
  43. return Int(index)
  44. }
  45. internal func throwRealmException(_ message: String, userInfo: [AnyHashable: Any]? = nil) {
  46. NSException(name: NSExceptionName(rawValue: RLMExceptionName), reason: message, userInfo: userInfo).raise()
  47. }
  48. internal func throwForNegativeIndex(_ int: Int, parameterName: String = "index") {
  49. if int < 0 {
  50. throwRealmException("Cannot pass a negative value for '\(parameterName)'.")
  51. }
  52. }
  53. internal func gsub(pattern: String, template: String, string: String, error: NSErrorPointer = nil) -> String? {
  54. let regex = try? NSRegularExpression(pattern: pattern, options: [])
  55. return regex?.stringByReplacingMatches(in: string, options: [],
  56. range: NSRange(location: 0, length: string.utf16.count),
  57. withTemplate: template)
  58. }
  59. internal func cast<U, V>(_ value: U, to: V.Type) -> V {
  60. if let v = value as? V {
  61. return v
  62. }
  63. return unsafeBitCast(value, to: to)
  64. }
  65. extension Object {
  66. // Must *only* be used to call Realm Objective-C APIs that are exposed on `RLMObject`
  67. // but actually operate on `RLMObjectBase`. Do not expose cast value to user.
  68. internal func unsafeCastToRLMObject() -> RLMObject {
  69. return unsafeBitCast(self, to: RLMObject.self)
  70. }
  71. }
  72. // MARK: CustomObjectiveCBridgeable
  73. internal func dynamicBridgeCast<T>(fromObjectiveC x: Any) -> T {
  74. if T.self == DynamicObject.self {
  75. return unsafeBitCast(x as AnyObject, to: T.self)
  76. } else if let bridgeableType = T.self as? CustomObjectiveCBridgeable.Type {
  77. return bridgeableType.bridging(objCValue: x) as! T
  78. } else {
  79. return x as! T
  80. }
  81. }
  82. internal func dynamicBridgeCast<T>(fromSwift x: T) -> Any {
  83. if let x = x as? CustomObjectiveCBridgeable {
  84. return x.objCValue
  85. } else {
  86. return x
  87. }
  88. }
  89. // Used for conversion from Objective-C types to Swift types
  90. internal protocol CustomObjectiveCBridgeable {
  91. static func bridging(objCValue: Any) -> Self
  92. var objCValue: Any { get }
  93. }
  94. // FIXME: needed with swift 3.2
  95. // Double isn't though?
  96. extension Float: CustomObjectiveCBridgeable {
  97. static func bridging(objCValue: Any) -> Float {
  98. return (objCValue as! NSNumber).floatValue
  99. }
  100. var objCValue: Any {
  101. return NSNumber(value: self)
  102. }
  103. }
  104. extension Int8: CustomObjectiveCBridgeable {
  105. static func bridging(objCValue: Any) -> Int8 {
  106. return (objCValue as! NSNumber).int8Value
  107. }
  108. var objCValue: Any {
  109. return NSNumber(value: self)
  110. }
  111. }
  112. extension Int16: CustomObjectiveCBridgeable {
  113. static func bridging(objCValue: Any) -> Int16 {
  114. return (objCValue as! NSNumber).int16Value
  115. }
  116. var objCValue: Any {
  117. return NSNumber(value: self)
  118. }
  119. }
  120. extension Int32: CustomObjectiveCBridgeable {
  121. static func bridging(objCValue: Any) -> Int32 {
  122. return (objCValue as! NSNumber).int32Value
  123. }
  124. var objCValue: Any {
  125. return NSNumber(value: self)
  126. }
  127. }
  128. extension Int64: CustomObjectiveCBridgeable {
  129. static func bridging(objCValue: Any) -> Int64 {
  130. return (objCValue as! NSNumber).int64Value
  131. }
  132. var objCValue: Any {
  133. return NSNumber(value: self)
  134. }
  135. }
  136. extension Optional: CustomObjectiveCBridgeable {
  137. static func bridging(objCValue: Any) -> Optional {
  138. if objCValue is NSNull {
  139. return nil
  140. } else {
  141. return .some(dynamicBridgeCast(fromObjectiveC: objCValue))
  142. }
  143. }
  144. var objCValue: Any {
  145. if let value = self {
  146. return dynamicBridgeCast(fromSwift: value)
  147. } else {
  148. return NSNull()
  149. }
  150. }
  151. }
  152. // MARK: AssistedObjectiveCBridgeable
  153. internal protocol AssistedObjectiveCBridgeable {
  154. static func bridging(from objectiveCValue: Any, with metadata: Any?) -> Self
  155. var bridged: (objectiveCValue: Any, metadata: Any?) { get }
  156. }