-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[NSMutableArray] replace objects in range docs/tests/impl #136
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
Closed
cullenbmacdonald
wants to merge
1
commit into
swiftlang:master
from
cullenbmacdonald:nsmutablearr-replace
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -658,7 +658,19 @@ public class NSMutableArray : NSArray { | |
} | ||
} | ||
} | ||
public func replaceObjectsInRange(range: NSRange, withObjectsFromArray otherArray: [AnyObject], range otherRange: NSRange) { NSUnimplemented() } | ||
|
||
/// Replaces the objects in the receiving array specified by one given range with the objects in another array specified by another range. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these docstrings are copy pasted from official docs. |
||
/// - parameter range: The range of objects to be replaced in (or removed from) the receiving array. | ||
/// - parameter otherArray: The array of objects from which to select replacements for the objects in `range`. | ||
/// - parameter otherRange: The range of objects be selected from `otherArray` as replacements for the objects in `range`. | ||
public func replaceObjectsInRange(range: NSRange, withObjectsFromArray otherArray: [AnyObject], range otherRange: NSRange) { | ||
let slicedOtherArray = Array(otherArray[otherRange.location...otherRange.length+otherRange.location]) | ||
replaceObjectsInRange(range, withObjectsFromArray: slicedOtherArray) | ||
} | ||
|
||
/// Replaces the objects in the receiving array specified by a given range with all of the objects from a given array. | ||
/// - parameter range: The range of objects to be replaced in (or removed from) the receiving array. | ||
/// - parameter otherArray: The array of objects from which to select replacements for the objects in `range`. | ||
public func replaceObjectsInRange(range: NSRange, withObjectsFromArray otherArray: [AnyObject]) { | ||
if self.dynamicType === NSMutableArray.self { | ||
_storage.reserveCapacity(count - range.length + otherArray.count) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See http://swift.org/LICENSE.txt for license information | ||
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
|
||
|
||
|
||
#if DEPLOYMENT_RUNTIME_OBJC || os(Linux) | ||
import Foundation | ||
import XCTest | ||
#else | ||
import SwiftFoundation | ||
import SwiftXCTest | ||
#endif | ||
|
||
|
||
|
||
class TestNSMutableArray : XCTestCase { | ||
|
||
var allTests : [(String, () -> ())] { | ||
return [ | ||
("test_replaceObjectsInRangeWithObjectsFromArray", test_replaceObjectsInRangeWithObjectsFromArray), | ||
] | ||
} | ||
|
||
func test_replaceObjectsInRangeWithObjectsFromArray() { | ||
let existing = [NSNumber(int: 1), NSNumber(int: 2), NSNumber(int: 3), NSNumber(int: 4), NSNumber(int: 5), NSNumber(int: 6)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is what is more verbose as a result of the bug i found. |
||
let nsExisting = NSMutableArray(array: existing) | ||
let otherArr = [NSNumber(int: 7), NSNumber(int: 8), NSNumber(int: 9)] | ||
let range = NSMakeRange(3, 3) | ||
nsExisting.replaceObjectsInRange(range, withObjectsFromArray: otherArr) | ||
XCTAssertEqual((nsExisting[0] as! NSNumber).intValue, 1) | ||
XCTAssertEqual((nsExisting[1] as! NSNumber).intValue, 2) | ||
XCTAssertEqual((nsExisting[2] as! NSNumber).intValue, 3) | ||
XCTAssertEqual((nsExisting[3] as! NSNumber).intValue, 7) | ||
XCTAssertEqual((nsExisting[4] as! NSNumber).intValue, 8) | ||
XCTAssertEqual((nsExisting[5] as! NSNumber).intValue, 9) | ||
} | ||
|
||
func test_replaceObjectsInRangeWithObjectsFromArrayOtherRange() { | ||
let existing = [NSNumber(int: 1), NSNumber(int: 2), NSNumber(int: 3), NSNumber(int: 4), NSNumber(int: 5), NSNumber(int: 6)] | ||
let nsExisting = NSMutableArray(array: existing) | ||
let otherArr = [NSNumber(int: 7), NSNumber(int: 8), NSNumber(int: 9), NSNumber(int: 1), NSNumber(int: 2), NSNumber(int: 3)] | ||
let range = NSMakeRange(3, 3) | ||
let otherRange = NSMakeRange(2, 3) | ||
nsExisting.replaceObjectsInRange(range, withObjectsFromArray: otherArr, range: otherRange) | ||
XCTAssertEqual((nsExisting[0] as! NSNumber).intValue, 1) | ||
XCTAssertEqual((nsExisting[1] as! NSNumber).intValue, 2) | ||
XCTAssertEqual((nsExisting[2] as! NSNumber).intValue, 9) | ||
XCTAssertEqual((nsExisting[3] as! NSNumber).intValue, 1) | ||
XCTAssertEqual((nsExisting[4] as! NSNumber).intValue, 2) | ||
XCTAssertEqual((nsExisting[5] as! NSNumber).intValue, 6) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
simply sorted the order here.