Skip to content

Commit 37cef8a

Browse files
committed
Merge pull request #7 from Adlai-Holler/FixArrayGetObjects
Use Correct Source Index in NSArray.getObjects
2 parents b5a7114 + 7d3aa28 commit 37cef8a

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

Foundation/NSArray.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,14 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
178178
/// - Experiment: This is a draft API currently under consideration for official import into Foundation
179179
/// - Note: Since this API is under consideration it may be either removed or revised in the near future
180180
public func getObjects(inout objects: [AnyObject], range: NSRange) {
181+
objects.reserveCapacity(objects.count + range.length)
182+
181183
if self.dynamicType === NSArray.self || self.dynamicType === NSMutableArray.self {
182-
if range.location == 0 && range.length == count {
183-
objects = _storage
184-
return
185-
}
186-
}
187-
for idx in 0..<range.length {
188-
objects[idx] = self[idx]
184+
objects += _storage[range.toRange()!]
185+
return
189186
}
187+
188+
objects += range.toRange()!.map { self[$0] }
190189
}
191190

192191
public func indexOfObject(anObject: AnyObject) -> Int {

TestFoundation/TestNSArray.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class TestNSArray : XCTestCase {
2626
("test_BasicConstruction", test_BasicConstruction),
2727
("test_enumeration", test_enumeration),
2828
("test_sequenceType", test_sequenceType),
29+
("test_getObjects", test_getObjects),
2930
]
3031
}
3132

@@ -70,4 +71,17 @@ class TestNSArray : XCTestCase {
7071
XCTAssertEqual(res, ["foo", "bar", "baz"])
7172
}
7273

74+
func test_getObjects() {
75+
let array : NSArray = ["foo", "bar", "baz", "foo1", "bar2", "baz3",].bridge()
76+
var objects = [AnyObject]()
77+
array.getObjects(&objects, range: NSMakeRange(1, 3))
78+
XCTAssertEqual(objects.count, 3)
79+
let fetched = [
80+
(objects[0] as! NSString).bridge(),
81+
(objects[1] as! NSString).bridge(),
82+
(objects[2] as! NSString).bridge(),
83+
]
84+
XCTAssertEqual(fetched, ["bar", "baz", "foo1"])
85+
}
86+
7387
}

0 commit comments

Comments
 (0)