From 1d272696741c0cab36f1d775764cbb8b19eb7ae4 Mon Sep 17 00:00:00 2001 From: Jason Flax Date: Tue, 26 Oct 2021 14:40:44 +0100 Subject: [PATCH 1/6] Fix ObjC support iteration --- Realm/Swift/RLMSupport.swift | 4 ++-- RealmSwift/Tests/ObjectiveCSupportTests.swift | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Realm/Swift/RLMSupport.swift b/Realm/Swift/RLMSupport.swift index ab3c840ab9..2ec596ec9b 100644 --- a/Realm/Swift/RLMSupport.swift +++ b/Realm/Swift/RLMSupport.swift @@ -112,8 +112,8 @@ public struct RLMCollectionIterator: IteratorProtocol { iteratorBase = NSFastEnumerationIterator(collection) } - public mutating func next() -> RLMObject? { - return iteratorBase.next() as! RLMObject? + public mutating func next() -> AnyObject? { + return iteratorBase.next() as AnyObject? } } diff --git a/RealmSwift/Tests/ObjectiveCSupportTests.swift b/RealmSwift/Tests/ObjectiveCSupportTests.swift index 9600b5d260..51c84c281e 100644 --- a/RealmSwift/Tests/ObjectiveCSupportTests.swift +++ b/RealmSwift/Tests/ObjectiveCSupportTests.swift @@ -131,4 +131,25 @@ class ObjectiveCSupportTests: TestCase { } expected.forEach { testObjCSupport($0.0, value: $0.1) } } + + func testArraySupport() { + let list = List() + let obj = SwiftObject() + list.append(obj) + obj.doubleCol = 42.42 + let rlmArray = ObjectiveCSupport.convert(object: list) + XCTAssert(rlmArray.isKind(of: RLMArray.self)) + for object in rlmArray { + XCTAssertEqual(obj.doubleCol, object.value(forKey: "doubleCol") as? Double) + } + + let primitiveList = List() + let double = 42.42 + primitiveList.append(double) + let primitiveRLMArray = ObjectiveCSupport.convert(object: primitiveList) + XCTAssert(primitiveRLMArray.isKind(of: RLMArray.self)) + for object in primitiveRLMArray { + XCTAssertEqual(double, object as? Double) + } + } } From 37c3de90ea23b11a69f5f456b8d2c03c861395a3 Mon Sep 17 00:00:00 2001 From: Jason Flax Date: Wed, 27 Oct 2021 11:17:10 +0100 Subject: [PATCH 2/6] Update CHANGELOG; Disallow new test to run on SPM --- CHANGELOG.md | 4 ++-- RealmSwift/Tests/ObjectiveCSupportTests.swift | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b78833a5a1..9a9facb396 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,8 @@ x.y.z Release notes (yyyy-MM-dd) * None. ### Fixed -* ([#????](https://github.com/realm/realm-cocoa/issues/????), since v?.?.?) -* None. +* Fix `RLMCollectionIterator` where `RLMCollectionIterator` contents were not RLMObject. + This is for users using the optional `RLMSupport.swift` file. diff --git a/RealmSwift/Tests/ObjectiveCSupportTests.swift b/RealmSwift/Tests/ObjectiveCSupportTests.swift index 51c84c281e..22fbf6f80f 100644 --- a/RealmSwift/Tests/ObjectiveCSupportTests.swift +++ b/RealmSwift/Tests/ObjectiveCSupportTests.swift @@ -132,6 +132,7 @@ class ObjectiveCSupportTests: TestCase { expected.forEach { testObjCSupport($0.0, value: $0.1) } } +#if !SWIFT_PACKAGE func testArraySupport() { let list = List() let obj = SwiftObject() @@ -152,4 +153,5 @@ class ObjectiveCSupportTests: TestCase { XCTAssertEqual(double, object as? Double) } } +#endif } From c7e5d07f13b19b8f183354ede04d39b2b595d75e Mon Sep 17 00:00:00 2001 From: Jason Flax Date: Wed, 27 Oct 2021 17:31:31 +0100 Subject: [PATCH 3/6] Add test displaying RLMArray iteratating over RLMEmbeddedObject --- RealmSwift/Tests/ObjectiveCSupportTests.swift | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/RealmSwift/Tests/ObjectiveCSupportTests.swift b/RealmSwift/Tests/ObjectiveCSupportTests.swift index 22fbf6f80f..5064259f20 100644 --- a/RealmSwift/Tests/ObjectiveCSupportTests.swift +++ b/RealmSwift/Tests/ObjectiveCSupportTests.swift @@ -134,24 +134,30 @@ class ObjectiveCSupportTests: TestCase { #if !SWIFT_PACKAGE func testArraySupport() { - let list = List() - let obj = SwiftObject() - list.append(obj) - obj.doubleCol = 42.42 - let rlmArray = ObjectiveCSupport.convert(object: list) - XCTAssert(rlmArray.isKind(of: RLMArray.self)) - for object in rlmArray { - XCTAssertEqual(obj.doubleCol, object.value(forKey: "doubleCol") as? Double) + func testType(_ firstValue: T) { + let list = List() + list.append(firstValue) + let rlmArray = ObjectiveCSupport.convert(object: list) + XCTAssert(rlmArray.isKind(of: RLMArray.self)) + for object in rlmArray { + XCTAssertEqual(firstValue, object as? T) + } } + testType(SwiftObject()) + testType(42.42) + testType("hello") + testType(EmbeddedTreeObject1()) + } - let primitiveList = List() - let double = 42.42 - primitiveList.append(double) - let primitiveRLMArray = ObjectiveCSupport.convert(object: primitiveList) - XCTAssert(primitiveRLMArray.isKind(of: RLMArray.self)) - for object in primitiveRLMArray { - XCTAssertEqual(double, object as? Double) + func testRLMArrayIterationForRLMEmbeddedObject() { + let rlmArrayEmbedded = RLMArray(objectClassName: EmbeddedIntObject.className()) + let obj = EmbeddedIntObject() + obj.intCol = 42 + rlmArrayEmbedded.add(obj) + for obj in rlmArrayEmbedded { + XCTAssertEqual(obj.intCol, 42) } } #endif } + From eb0fddd138d4a42d76949eb144195c2aabfda204 Mon Sep 17 00:00:00 2001 From: Jason Flax Date: Wed, 27 Oct 2021 17:33:01 +0100 Subject: [PATCH 4/6] Appease linter --- RealmSwift/Tests/ObjectiveCSupportTests.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/RealmSwift/Tests/ObjectiveCSupportTests.swift b/RealmSwift/Tests/ObjectiveCSupportTests.swift index 5064259f20..106c316ba8 100644 --- a/RealmSwift/Tests/ObjectiveCSupportTests.swift +++ b/RealmSwift/Tests/ObjectiveCSupportTests.swift @@ -160,4 +160,3 @@ class ObjectiveCSupportTests: TestCase { } #endif } - From 5b2c2c8a37914dd0a3afbfc6c66c2ff048ac7ca8 Mon Sep 17 00:00:00 2001 From: Jason Flax Date: Thu, 28 Oct 2021 14:44:42 +0100 Subject: [PATCH 5/6] Add support test to correct location; modify it --- Realm/Tests/Swift/SwiftArrayTests.swift | 25 +++++++++++++++++ RealmSwift/Tests/ObjectiveCSupportTests.swift | 28 ------------------- 2 files changed, 25 insertions(+), 28 deletions(-) diff --git a/Realm/Tests/Swift/SwiftArrayTests.swift b/Realm/Tests/Swift/SwiftArrayTests.swift index 4b4057fa1d..a7772d253e 100644 --- a/Realm/Tests/Swift/SwiftArrayTests.swift +++ b/Realm/Tests/Swift/SwiftArrayTests.swift @@ -513,4 +513,29 @@ class SwiftRLMArrayTests: RLMTestCase { XCTAssertEqual(Int32(20), (sortedByAge[0] as! EmployeeObject).age) XCTAssertEqual(Int32(40), (sortedByName[0] as! EmployeeObject).age) } + + func testRLMArrayIterationForObjectAndNonObjectTypes() { + let rlmArrayEmbedded = RLMArray(objectClassName: EmbeddedIntObject.className()) + let obj = EmbeddedIntObject() + obj.intCol = 42 + rlmArrayEmbedded.add(obj) + for obj in rlmArrayEmbedded { + XCTAssertEqual(obj.intCol, 42) + } + + let rlmArrayObject = RLMArray(objectClassName: IntObject.className()) + let intObject = IntObject() + intObject.intCol = 42 + rlmArrayObject.add(intObject) + for obj in rlmArrayObject { + XCTAssertEqual(obj.intCol, 42) + } + + let rlmArrayPrimitive = RLMArray(objectClassName: NSString.className()) + let str = "foo" + rlmArrayPrimitive.add(str as NSString) + for obj in rlmArrayPrimitive { + XCTAssertEqual(obj as! NSString, "foo" as NSString) + } + } } diff --git a/RealmSwift/Tests/ObjectiveCSupportTests.swift b/RealmSwift/Tests/ObjectiveCSupportTests.swift index 106c316ba8..9600b5d260 100644 --- a/RealmSwift/Tests/ObjectiveCSupportTests.swift +++ b/RealmSwift/Tests/ObjectiveCSupportTests.swift @@ -131,32 +131,4 @@ class ObjectiveCSupportTests: TestCase { } expected.forEach { testObjCSupport($0.0, value: $0.1) } } - -#if !SWIFT_PACKAGE - func testArraySupport() { - func testType(_ firstValue: T) { - let list = List() - list.append(firstValue) - let rlmArray = ObjectiveCSupport.convert(object: list) - XCTAssert(rlmArray.isKind(of: RLMArray.self)) - for object in rlmArray { - XCTAssertEqual(firstValue, object as? T) - } - } - testType(SwiftObject()) - testType(42.42) - testType("hello") - testType(EmbeddedTreeObject1()) - } - - func testRLMArrayIterationForRLMEmbeddedObject() { - let rlmArrayEmbedded = RLMArray(objectClassName: EmbeddedIntObject.className()) - let obj = EmbeddedIntObject() - obj.intCol = 42 - rlmArrayEmbedded.add(obj) - for obj in rlmArrayEmbedded { - XCTAssertEqual(obj.intCol, 42) - } - } -#endif } From dd884bfe4f630d9d5ab3c1623764d671c2c28c08 Mon Sep 17 00:00:00 2001 From: Jason Flax Date: Fri, 29 Oct 2021 13:03:24 +0100 Subject: [PATCH 6/6] Fix test that did not compile --- Realm/Tests/Swift/SwiftArrayTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Realm/Tests/Swift/SwiftArrayTests.swift b/Realm/Tests/Swift/SwiftArrayTests.swift index a7772d253e..a5fcd90d4a 100644 --- a/Realm/Tests/Swift/SwiftArrayTests.swift +++ b/Realm/Tests/Swift/SwiftArrayTests.swift @@ -531,7 +531,7 @@ class SwiftRLMArrayTests: RLMTestCase { XCTAssertEqual(obj.intCol, 42) } - let rlmArrayPrimitive = RLMArray(objectClassName: NSString.className()) + let rlmArrayPrimitive = RLMArray(objectClassName: "NSString") let str = "foo" rlmArrayPrimitive.add(str as NSString) for obj in rlmArrayPrimitive {