Util.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. /// :nodoc:
  74. public func dynamicBridgeCast<T>(fromObjectiveC x: Any) -> T {
  75. if T.self == DynamicObject.self {
  76. return unsafeBitCast(x as AnyObject, to: T.self)
  77. } else if let bridgeableType = T.self as? CustomObjectiveCBridgeable.Type {
  78. return bridgeableType.bridging(objCValue: x) as! T
  79. } else {
  80. return x as! T
  81. }
  82. }
  83. /// :nodoc:
  84. public func dynamicBridgeCast<T>(fromSwift x: T) -> Any {
  85. if let x = x as? CustomObjectiveCBridgeable {
  86. return x.objCValue
  87. } else {
  88. return x
  89. }
  90. }
  91. // Used for conversion from Objective-C types to Swift types
  92. internal protocol CustomObjectiveCBridgeable {
  93. static func bridging(objCValue: Any) -> Self
  94. var objCValue: Any { get }
  95. }
  96. // FIXME: needed with swift 3.2
  97. // Double isn't though?
  98. extension Float: CustomObjectiveCBridgeable {
  99. static func bridging(objCValue: Any) -> Float {
  100. return (objCValue as! NSNumber).floatValue
  101. }
  102. var objCValue: Any {
  103. return NSNumber(value: self)
  104. }
  105. }
  106. extension Int8: CustomObjectiveCBridgeable {
  107. static func bridging(objCValue: Any) -> Int8 {
  108. return (objCValue as! NSNumber).int8Value
  109. }
  110. var objCValue: Any {
  111. return NSNumber(value: self)
  112. }
  113. }
  114. extension Int16: CustomObjectiveCBridgeable {
  115. static func bridging(objCValue: Any) -> Int16 {
  116. return (objCValue as! NSNumber).int16Value
  117. }
  118. var objCValue: Any {
  119. return NSNumber(value: self)
  120. }
  121. }
  122. extension Int32: CustomObjectiveCBridgeable {
  123. static func bridging(objCValue: Any) -> Int32 {
  124. return (objCValue as! NSNumber).int32Value
  125. }
  126. var objCValue: Any {
  127. return NSNumber(value: self)
  128. }
  129. }
  130. extension Int64: CustomObjectiveCBridgeable {
  131. static func bridging(objCValue: Any) -> Int64 {
  132. return (objCValue as! NSNumber).int64Value
  133. }
  134. var objCValue: Any {
  135. return NSNumber(value: self)
  136. }
  137. }
  138. extension Optional: CustomObjectiveCBridgeable {
  139. static func bridging(objCValue: Any) -> Optional {
  140. if objCValue is NSNull {
  141. return nil
  142. } else {
  143. return .some(dynamicBridgeCast(fromObjectiveC: objCValue))
  144. }
  145. }
  146. var objCValue: Any {
  147. if let value = self {
  148. return dynamicBridgeCast(fromSwift: value)
  149. } else {
  150. return NSNull()
  151. }
  152. }
  153. }
  154. // MARK: AssistedObjectiveCBridgeable
  155. internal protocol AssistedObjectiveCBridgeable {
  156. static func bridging(from objectiveCValue: Any, with metadata: Any?) -> Self
  157. var bridged: (objectiveCValue: Any, metadata: Any?) { get }
  158. }