Skip to content

Use Correct Source Index in NSArray.getObjects #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions Foundation/NSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,14 @@ public class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NS
/// - Experiment: This is a draft API currently under consideration for official import into Foundation
/// - Note: Since this API is under consideration it may be either removed or revised in the near future
public func getObjects(inout objects: [AnyObject], range: NSRange) {
objects.reserveCapacity(objects.count + range.length)

if self.dynamicType === NSArray.self || self.dynamicType === NSMutableArray.self {
if range.location == 0 && range.length == count {
objects = _storage
return
}
}
for idx in 0..<range.length {
objects[idx] = self[idx]
objects += _storage[range.toRange()!]
return
}

objects += range.toRange()!.map { self[$0] }
}

public func indexOfObject(anObject: AnyObject) -> Int {
Expand Down
14 changes: 14 additions & 0 deletions TestFoundation/TestNSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class TestNSArray : XCTestCase {
("test_BasicConstruction", test_BasicConstruction),
("test_enumeration", test_enumeration),
("test_sequenceType", test_sequenceType),
("test_getObjects", test_getObjects),
]
}

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

func test_getObjects() {
let array : NSArray = ["foo", "bar", "baz", "foo1", "bar2", "baz3",].bridge()
var objects = [AnyObject]()
array.getObjects(&objects, range: NSMakeRange(1, 3))
XCTAssertEqual(objects.count, 3)
let fetched = [
(objects[0] as! NSString).bridge(),
(objects[1] as! NSString).bridge(),
(objects[2] as! NSString).bridge(),
]
XCTAssertEqual(fetched, ["bar", "baz", "foo1"])
}

}