Skip to content

NSMutableArray replaceObjectsInRange:withObjectsFromArray:range: #125

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 7 commits into from
Dec 16, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion Foundation/NSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,10 @@ public class NSMutableArray : NSArray {
}
}
}
public func replaceObjectsInRange(range: NSRange, withObjectsFromArray otherArray: [AnyObject], range otherRange: NSRange) { NSUnimplemented() }
public func replaceObjectsInRange(range: NSRange, withObjectsFromArray otherArray: [AnyObject], range otherRange: NSRange) {
replaceObjectsInRange(range, withObjectsFromArray: otherArray.bridge().subarrayWithRange(otherRange))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the bridge here is not needed since otherArray is already an Array<AnyObject>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I'm on it.

}

public func replaceObjectsInRange(range: NSRange, withObjectsFromArray otherArray: [AnyObject]) {
if self.dynamicType === NSMutableArray.self {
_storage.reserveCapacity(count - range.length + otherArray.count)
Expand Down
22 changes: 21 additions & 1 deletion TestFoundation/TestNSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class TestNSArray : XCTestCase {
("test_enumeration", test_enumeration),
("test_sequenceType", test_sequenceType),
("test_getObjects", test_getObjects),
("test_binarySearch", test_binarySearch)
("test_binarySearch", test_binarySearch),
("test_replaceObjectsInRange_withObjectsFromArray_range", test_replaceObjectsInRange_withObjectsFromArray_range),
]
}

Expand Down Expand Up @@ -148,4 +149,23 @@ class TestNSArray : XCTestCase {

return .OrderedDescending
}

func test_replaceObjectsInRange_withObjectsFromArray_range() {
let array1 = NSMutableArray(array:[
"foo1".bridge(),
"bar1".bridge(),
"baz1".bridge()])

let array2: [AnyObject] = [
"foo2".bridge(),
"bar2".bridge(),
"baz2".bridge()]

array1.replaceObjectsInRange(NSMakeRange(1, 1), withObjectsFromArray: array2, range: NSMakeRange(1, 2))

XCTAssertEqual(array1[0] as? NSString, "foo1".bridge(), "Expected foo1 but was \(array1[0])")
XCTAssertEqual(array1[1] as? NSString, "bar2".bridge(), "Expected bar2 but was \(array1[1])")
XCTAssertEqual(array1[2] as? NSString, "baz2".bridge(), "Expected baz2 but was \(array1[2])")
XCTAssertEqual(array1[3] as? NSString, "baz1".bridge(), "Expected baz1 but was \(array1[3])")
}
}