ObjectiveCSupport.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 Realm
  19. /**
  20. `ObjectiveCSupport` is a class providing methods for Swift/Objective-C interoperability.
  21. With `ObjectiveCSupport` you can either retrieve the internal ObjC representations of the Realm objects,
  22. or wrap ObjC Realm objects with their Swift equivalents.
  23. Use this to provide public APIs that support both platforms.
  24. :nodoc:
  25. **/
  26. public struct ObjectiveCSupport {
  27. /// Convert a `Results` to a `RLMResults`.
  28. public static func convert<T>(object: Results<T>) -> RLMResults<AnyObject> {
  29. return object.rlmResults
  30. }
  31. /// Convert a `RLMResults` to a `Results`.
  32. public static func convert(object: RLMResults<AnyObject>) -> Results<Object> {
  33. return Results(object)
  34. }
  35. /// Convert a `List` to a `RLMArray`.
  36. public static func convert<T>(object: List<T>) -> RLMArray<AnyObject> {
  37. return object._rlmArray
  38. }
  39. /// Convert a `RLMArray` to a `List`.
  40. public static func convert(object: RLMArray<AnyObject>) -> List<Object> {
  41. return List(rlmArray: object)
  42. }
  43. /// Convert a `LinkingObjects` to a `RLMResults`.
  44. public static func convert<T>(object: LinkingObjects<T>) -> RLMResults<AnyObject> {
  45. return object.rlmResults
  46. }
  47. /// Convert a `RLMLinkingObjects` to a `Results`.
  48. public static func convert(object: RLMLinkingObjects<RLMObject>) -> Results<Object> {
  49. return Results(object)
  50. }
  51. /// Convert a `Realm` to a `RLMRealm`.
  52. public static func convert(object: Realm) -> RLMRealm {
  53. return object.rlmRealm
  54. }
  55. /// Convert a `RLMRealm` to a `Realm`.
  56. public static func convert(object: RLMRealm) -> Realm {
  57. return Realm(object)
  58. }
  59. /// Convert a `Migration` to a `RLMMigration`.
  60. public static func convert(object: Migration) -> RLMMigration {
  61. return object.rlmMigration
  62. }
  63. /// Convert a `RLMMigration` to a `Migration`.
  64. public static func convert(object: RLMMigration) -> Migration {
  65. return Migration(object)
  66. }
  67. /// Convert a `ObjectSchema` to a `RLMObjectSchema`.
  68. public static func convert(object: ObjectSchema) -> RLMObjectSchema {
  69. return object.rlmObjectSchema
  70. }
  71. /// Convert a `RLMObjectSchema` to a `ObjectSchema`.
  72. public static func convert(object: RLMObjectSchema) -> ObjectSchema {
  73. return ObjectSchema(object)
  74. }
  75. /// Convert a `Property` to a `RLMProperty`.
  76. public static func convert(object: Property) -> RLMProperty {
  77. return object.rlmProperty
  78. }
  79. /// Convert a `RLMProperty` to a `Property`.
  80. public static func convert(object: RLMProperty) -> Property {
  81. return Property(object)
  82. }
  83. /// Convert a `Realm.Configuration` to a `RLMRealmConfiguration`.
  84. public static func convert(object: Realm.Configuration) -> RLMRealmConfiguration {
  85. return object.rlmConfiguration
  86. }
  87. /// Convert a `RLMRealmConfiguration` to a `Realm.Configuration`.
  88. public static func convert(object: RLMRealmConfiguration) -> Realm.Configuration {
  89. return .fromRLMRealmConfiguration(object)
  90. }
  91. /// Convert a `Schema` to a `RLMSchema`.
  92. public static func convert(object: Schema) -> RLMSchema {
  93. return object.rlmSchema
  94. }
  95. /// Convert a `RLMSchema` to a `Schema`.
  96. public static func convert(object: RLMSchema) -> Schema {
  97. return Schema(object)
  98. }
  99. /// Convert a `SortDescriptor` to a `RLMSortDescriptor`.
  100. public static func convert(object: SortDescriptor) -> RLMSortDescriptor {
  101. return object.rlmSortDescriptorValue
  102. }
  103. /// Convert a `RLMSortDescriptor` to a `SortDescriptor`.
  104. public static func convert(object: RLMSortDescriptor) -> SortDescriptor {
  105. return SortDescriptor(keyPath: object.keyPath, ascending: object.ascending)
  106. }
  107. /// Convert a `RLMShouldCompactOnLaunchBlock` to a Realm Swift compact block.
  108. public static func convert(object: @escaping RLMShouldCompactOnLaunchBlock) -> (Int, Int) -> Bool {
  109. return { totalBytes, usedBytes in
  110. return object(UInt(totalBytes), UInt(usedBytes))
  111. }
  112. }
  113. /// Convert a Realm Swift compact block to a `RLMShouldCompactOnLaunchBlock`.
  114. public static func convert(object: @escaping (Int, Int) -> Bool) -> RLMShouldCompactOnLaunchBlock {
  115. return { totalBytes, usedBytes in
  116. return object(Int(totalBytes), Int(usedBytes))
  117. }
  118. }
  119. }