Merge pull request #594 from itaiferber/pr-ordered-set-filtering
Correct NSOrderedSet filtering by unboxing objects
diff --git a/Foundation/NSOrderedSet.swift b/Foundation/NSOrderedSet.swift
index 99d775b..2146083 100644
--- a/Foundation/NSOrderedSet.swift
+++ b/Foundation/NSOrderedSet.swift
@@ -120,6 +120,16 @@
_insertObject(obj)
}
}
+
+ internal var allObjects: [Any] {
+ if type(of: self) === NSOrderedSet.self || type(of: self) === NSMutableOrderedSet.self {
+ return _orderedStorage.map { _SwiftValue.fetch($0) }
+ } else {
+ return (0..<count).map { idx in
+ return self[idx]
+ }
+ }
+ }
}
extension NSOrderedSet : Sequence {
diff --git a/Foundation/NSPredicate.swift b/Foundation/NSPredicate.swift
index 14c8908..083782b 100644
--- a/Foundation/NSPredicate.swift
+++ b/Foundation/NSPredicate.swift
@@ -127,7 +127,7 @@
extension NSOrderedSet {
open func filtered(using predicate: NSPredicate) -> NSOrderedSet {
- return NSOrderedSet(array: self._orderedStorage.filter({ object in
+ return NSOrderedSet(array: self.allObjects.filter({ object in
return predicate.evaluate(with: object)
}))
}
diff --git a/TestFoundation/TestNSPredicate.swift b/TestFoundation/TestNSPredicate.swift
index 7b2739f..4275d9a 100644
--- a/TestFoundation/TestNSPredicate.swift
+++ b/TestFoundation/TestNSPredicate.swift
@@ -21,7 +21,7 @@
return [
("test_BooleanPredicate", test_BooleanPredicate),
("test_BlockPredicateWithoutVariableBindings", test_BlockPredicateWithoutVariableBindings),
-// ("test_filterNSArray", test_filterNSArray),
+ ("test_filterNSArray", test_filterNSArray),
("test_filterNSMutableArray", test_filterNSMutableArray),
("test_filterNSSet", test_filterNSSet),
("test_filterNSMutableSet", test_filterNSMutableSet),
@@ -57,7 +57,6 @@
func test_filterNSArray() {
let filteredArray = NSArray(array: startArray).filtered(using: lengthLessThanThreePredicate).map { $0 as! String }
-
XCTAssertEqual(expectedArray, filteredArray)
}
@@ -81,28 +80,18 @@
}
func test_filterNSOrderedSet() {
- // TODO
- // This test is temporarily disabled due to a compile crash when calling the initializer of NSOrderedSet with an array
- /*
let orderedSet = NSOrderedSet(array: startArray)
- let filteredOrderedSet = orderedSet.filteredOrderedSetUsingPredicate(lengthLessThanThreePredicate)
-
+ let filteredOrderedSet = orderedSet.filtered(using: lengthLessThanThreePredicate)
XCTAssertEqual(NSOrderedSet(array: expectedArray), filteredOrderedSet)
- */
}
func test_filterNSMutableOrderedSet() {
- // TODO
- // This test is temporarily disabled due to a compile crash when calling the initializer of NSOrderedSet with an array
- /*
let orderedSet = NSMutableOrderedSet()
- orderedSet.addObjectsFromArray(startArray)
-
- orderedSet.filterUsingPredicate(lengthLessThanThreePredicate)
+ orderedSet.addObjects(from: startArray)
+ orderedSet.filter(using: lengthLessThanThreePredicate)
let expectedOrderedSet = NSMutableOrderedSet()
- expectedOrderedSet.addObjectsFromArray(expectedArray)
+ expectedOrderedSet.addObjects(from: expectedArray)
XCTAssertEqual(expectedOrderedSet, orderedSet)
- */
}
}