RLMSupport.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2014 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. //
  19. // SPDX-FileCopyrightText: 2014 Realm Inc.
  20. // SPDX-License-Identifier: Apache-2.0
  21. //
  22. import Realm
  23. extension RLMRealm {
  24. /**
  25. Returns the schema version for a Realm at a given local URL.
  26. - see: `+ [RLMRealm schemaVersionAtURL:encryptionKey:error:]`
  27. */
  28. @nonobjc public class func schemaVersion(at url: URL, usingEncryptionKey key: Data? = nil) throws -> UInt64 {
  29. var error: NSError?
  30. let version = __schemaVersion(at: url, encryptionKey: key, error: &error)
  31. guard version != RLMNotVersioned else { throw error! }
  32. return version
  33. }
  34. /**
  35. Returns the same object as the one referenced when the `RLMThreadSafeReference` was first created,
  36. but resolved for the current Realm for this thread. Returns `nil` if this object was deleted after
  37. the reference was created.
  38. - see `- [RLMRealm resolveThreadSafeReference:]`
  39. */
  40. @nonobjc public func resolve<Confined>(reference: RLMThreadSafeReference<Confined>) -> Confined? {
  41. return __resolve(reference as! RLMThreadSafeReference<RLMThreadConfined>) as! Confined?
  42. }
  43. }
  44. extension RLMObject {
  45. /**
  46. Returns all objects of this object type matching the given predicate from the default Realm.
  47. - see `+ [RLMObject objectsWithPredicate:]`
  48. */
  49. public class func objects(where predicateFormat: String, _ args: CVarArg...) -> RLMResults<RLMObject> {
  50. return objects(with: NSPredicate(format: predicateFormat, arguments: getVaList(args))) as! RLMResults<RLMObject>
  51. }
  52. /**
  53. Returns all objects of this object type matching the given predicate from the specified Realm.
  54. - see `+ [RLMObject objectsInRealm:withPredicate:]`
  55. */
  56. public class func objects(in realm: RLMRealm,
  57. where predicateFormat: String,
  58. _ args: CVarArg...) -> RLMResults<RLMObject> {
  59. return objects(in: realm, with: NSPredicate(format: predicateFormat, arguments: getVaList(args))) as! RLMResults<RLMObject>
  60. }
  61. }
  62. /// A protocol defining iterator support for RLMArray, RLMSet & RLMResults.
  63. public protocol _RLMCollectionIterator {
  64. /**
  65. Returns a `RLMCollectionIterator` that yields successive elements in the collection.
  66. This enables support for sequence-style enumeration of `RLMObject` subclasses in Swift.
  67. */
  68. func makeIterator() -> RLMCollectionIterator
  69. }
  70. extension _RLMCollectionIterator where Self: RLMCollection {
  71. /// :nodoc:
  72. public func makeIterator() -> RLMCollectionIterator {
  73. return RLMCollectionIterator(self)
  74. }
  75. }
  76. /// :nodoc:
  77. public typealias RLMDictionarySingleEntry = (key: String, value: RLMObject)
  78. /// A protocol defining iterator support for RLMDictionary
  79. public protocol _RLMDictionaryIterator {
  80. /// :nodoc:
  81. func makeIterator() -> RLMDictionaryIterator
  82. }
  83. extension _RLMDictionaryIterator where Self: RLMCollection {
  84. /// :nodoc:
  85. public func makeIterator() -> RLMDictionaryIterator {
  86. return RLMDictionaryIterator(self)
  87. }
  88. }
  89. // Sequence conformance for RLMArray, RLMDictionary, RLMSet and RLMResults is provided by RLMCollection's
  90. // `makeIterator()` implementation.
  91. extension RLMArray: Sequence, _RLMCollectionIterator { }
  92. extension RLMDictionary: Sequence, _RLMDictionaryIterator {}
  93. extension RLMSet: Sequence, _RLMCollectionIterator {}
  94. extension RLMResults: Sequence, _RLMCollectionIterator {}
  95. /**
  96. This struct enables sequence-style enumeration for RLMObjects in Swift via `RLMCollection.makeIterator`
  97. */
  98. public struct RLMCollectionIterator: IteratorProtocol {
  99. private var iteratorBase: NSFastEnumerationIterator
  100. internal init(_ collection: RLMCollection) {
  101. iteratorBase = NSFastEnumerationIterator(collection)
  102. }
  103. public mutating func next() -> RLMObject? {
  104. return iteratorBase.next() as! RLMObject?
  105. }
  106. }
  107. /**
  108. This struct enables sequence-style enumeration for RLMDictionary in Swift via `RLMDictionary.makeIterator`
  109. */
  110. public struct RLMDictionaryIterator: IteratorProtocol {
  111. private var iteratorBase: NSFastEnumerationIterator
  112. private let dictionary: RLMDictionary<AnyObject, AnyObject>
  113. internal init(_ collection: RLMCollection) {
  114. dictionary = collection as! RLMDictionary<AnyObject, AnyObject>
  115. iteratorBase = NSFastEnumerationIterator(collection)
  116. }
  117. public mutating func next() -> RLMDictionarySingleEntry? {
  118. let key = iteratorBase.next()
  119. if let key = key {
  120. return (key: key as Any, value: dictionary[key as AnyObject]) as? RLMDictionarySingleEntry
  121. }
  122. if key != nil {
  123. fatalError("unsupported key type")
  124. }
  125. return nil
  126. }
  127. }
  128. // Swift query convenience functions
  129. extension RLMCollection {
  130. /**
  131. Returns the index of the first object in the collection matching the predicate.
  132. */
  133. public func indexOfObject(where predicateFormat: String, _ args: CVarArg...) -> UInt {
  134. guard let index = indexOfObject?(with: NSPredicate(format: predicateFormat, arguments: getVaList(args))) else {
  135. fatalError("This RLMCollection does not support indexOfObject(where:)")
  136. }
  137. return index
  138. }
  139. /**
  140. Returns all objects matching the given predicate in the collection.
  141. */
  142. public func objects(where predicateFormat: String, _ args: CVarArg...) -> RLMResults<NSObject> {
  143. return objects(with: NSPredicate(format: predicateFormat, arguments: getVaList(args))) as! RLMResults<NSObject>
  144. }
  145. }
  146. extension RLMCollection {
  147. /// Allows for subscript support with RLMDictionary.
  148. public subscript(_ key: String) -> AnyObject? {
  149. get {
  150. (self as! RLMDictionary<NSString, AnyObject>).object(forKey: key as NSString)
  151. }
  152. set {
  153. (self as! RLMDictionary<NSString, AnyObject>).setObject(newValue, forKey: key as NSString)
  154. }
  155. }
  156. }