Skip to content

Commit c70f7bc

Browse files
[NSMutableArray] replace objects in range docs/tests/impl
1 parent 281aa0f commit c70f7bc

File tree

4 files changed

+87
-12
lines changed

4 files changed

+87
-12
lines changed

Foundation.xcodeproj/project.pbxproj

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@
203203
61F8AE7D1C180FC600FB62F0 /* TestNSNotificationCenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61F8AE7C1C180FC600FB62F0 /* TestNSNotificationCenter.swift */; };
204204
6E203B8D1C1303BB003B2576 /* TestNSBundle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E203B8C1C1303BB003B2576 /* TestNSBundle.swift */; };
205205
7A7D6FBB1C16439400957E2E /* TestNSURLResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A7D6FBA1C16439400957E2E /* TestNSURLResponse.swift */; };
206+
7AD22BF01C1CE0AA00377EC0 /* TestNSMutableArray.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AD22BEF1C1CE0AA00377EC0 /* TestNSMutableArray.swift */; };
206207
83712C8E1C1684900049AD49 /* TestNSURLRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83712C8D1C1684900049AD49 /* TestNSURLRequest.swift */; };
207208
844DC3331C17584F005611F9 /* TestNSScanner.swift in Sources */ = {isa = PBXBuildFile; fileRef = 844DC3321C17584F005611F9 /* TestNSScanner.swift */; };
208209
848A30581C137B3500C83206 /* TestNSHTTPCookie.swift in Sources */ = {isa = PBXBuildFile; fileRef = 848A30571C137B3500C83206 /* TestNSHTTPCookie.swift */; };
@@ -543,6 +544,7 @@
543544
61F8AE7C1C180FC600FB62F0 /* TestNSNotificationCenter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSNotificationCenter.swift; sourceTree = "<group>"; };
544545
6E203B8C1C1303BB003B2576 /* TestNSBundle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSBundle.swift; sourceTree = "<group>"; };
545546
7A7D6FBA1C16439400957E2E /* TestNSURLResponse.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSURLResponse.swift; sourceTree = "<group>"; };
547+
7AD22BEF1C1CE0AA00377EC0 /* TestNSMutableArray.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSMutableArray.swift; sourceTree = "<group>"; };
546548
83712C8D1C1684900049AD49 /* TestNSURLRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSURLRequest.swift; sourceTree = "<group>"; };
547549
844DC3321C17584F005611F9 /* TestNSScanner.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSScanner.swift; sourceTree = "<group>"; };
548550
848A30571C137B3500C83206 /* TestNSHTTPCookie.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = TestNSHTTPCookie.swift; path = TestFoundation/TestNSHTTPCookie.swift; sourceTree = SOURCE_ROOT; };
@@ -1043,37 +1045,38 @@
10431045
EA66F65A1BF1976100136161 /* Tests */ = {
10441046
isa = PBXGroup;
10451047
children = (
1046-
61F8AE7C1C180FC600FB62F0 /* TestNSNotificationCenter.swift */,
10471048
C93559281C12C49F009FD6A9 /* TestNSAffineTransform.swift */,
10481049
EA66F63C1BF1619600136161 /* TestNSArray.swift */,
10491050
6E203B8C1C1303BB003B2576 /* TestNSBundle.swift */,
1050-
5BC1D8BC1BF3ADFE009D3973 /* TestNSCharacterSet.swift */,
1051+
A5A34B551C18C85D00FD972B /* TestNSByteCountFormatter.swift */,
10511052
52829AD61C160D64003BC4EF /* TestNSCalendar.swift */,
1053+
5BC1D8BC1BF3ADFE009D3973 /* TestNSCharacterSet.swift */,
1054+
DCDBB8321C1768AC00313299 /* TestNSData.swift */,
1055+
22B9C1E01C165D7A00DECFF9 /* TestNSDate.swift */,
10521056
EA66F63D1BF1619600136161 /* TestNSDictionary.swift */,
1053-
848A30571C137B3500C83206 /* TestNSHTTPCookie.swift */,
10541057
525AECEB1BF2C96400D15BB0 /* TestNSFileManager.swift */,
10551058
88D28DE61C13AE9000494606 /* TestNSGeometry.swift */,
1059+
848A30571C137B3500C83206 /* TestNSHTTPCookie.swift */,
10561060
4AE109261C17CCBF007367B5 /* TestNSIndexPath.swift */,
10571061
EA66F63E1BF1619600136161 /* TestNSIndexSet.swift */,
10581062
5EB6A15C1C188FC40037DCB8 /* TestNSJSONSerialization.swift */,
1063+
7AD22BEF1C1CE0AA00377EC0 /* TestNSMutableArray.swift */,
1064+
61F8AE7C1C180FC600FB62F0 /* TestNSNotificationCenter.swift */,
1065+
612952F81C1B235900BE0FD9 /* TestNSNull.swift */,
10591066
EA66F63F1BF1619600136161 /* TestNSNumber.swift */,
10601067
4DC1D07F1C12EEEF00B5948A /* TestNSPipe.swift */,
10611068
400E22641C1A4E58007C5933 /* TestNSProcessInfo.swift */,
10621069
EA66F6401BF1619600136161 /* TestNSPropertyList.swift */,
10631070
E876A73D1C1180E000F279EC /* TestNSRange.swift */,
1071+
844DC3321C17584F005611F9 /* TestNSScanner.swift */,
10641072
EA66F6411BF1619600136161 /* TestNSSet.swift */,
10651073
EA66F6421BF1619600136161 /* TestNSString.swift */,
1066-
C2A9D75B1C15C08B00993803 /* TestNSUUID.swift */,
1067-
EA66F6431BF1619600136161 /* TestNSURL.swift */,
1068-
5B40F9F11C125187000E72E3 /* TestNSXMLParser.swift */,
1069-
22B9C1E01C165D7A00DECFF9 /* TestNSDate.swift */,
1070-
DCDBB8321C1768AC00313299 /* TestNSData.swift */,
10711074
84BA558D1C16F90900F48C54 /* TestNSTimeZone.swift */,
1072-
844DC3321C17584F005611F9 /* TestNSScanner.swift */,
1075+
EA66F6431BF1619600136161 /* TestNSURL.swift */,
10731076
83712C8D1C1684900049AD49 /* TestNSURLRequest.swift */,
10741077
7A7D6FBA1C16439400957E2E /* TestNSURLResponse.swift */,
1075-
A5A34B551C18C85D00FD972B /* TestNSByteCountFormatter.swift */,
1076-
612952F81C1B235900BE0FD9 /* TestNSNull.swift */,
1078+
C2A9D75B1C15C08B00993803 /* TestNSUUID.swift */,
1079+
5B40F9F11C125187000E72E3 /* TestNSXMLParser.swift */,
10771080
);
10781081
name = Tests;
10791082
sourceTree = "<group>";
@@ -1796,6 +1799,7 @@
17961799
88D28DE71C13AE9000494606 /* TestNSGeometry.swift in Sources */,
17971800
EA66F64C1BF1619600136161 /* TestNSDictionary.swift in Sources */,
17981801
ED58F76F1C134B3A00E6A5BE /* (null) in Sources */,
1802+
7AD22BF01C1CE0AA00377EC0 /* TestNSMutableArray.swift in Sources */,
17991803
EA66F6581BF1619600136161 /* TestNSURL.swift in Sources */,
18001804
EA66F6441BF1619600136161 /* main.swift in Sources */,
18011805
);

Foundation/NSArray.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,19 @@ public class NSMutableArray : NSArray {
658658
}
659659
}
660660
}
661-
public func replaceObjectsInRange(range: NSRange, withObjectsFromArray otherArray: [AnyObject], range otherRange: NSRange) { NSUnimplemented() }
661+
662+
/// Replaces the objects in the receiving array specified by one given range with the objects in another array specified by another range.
663+
/// - parameter range: The range of objects to be replaced in (or removed from) the receiving array.
664+
/// - parameter otherArray: The array of objects from which to select replacements for the objects in `range`.
665+
/// - parameter otherRange: The range of objects be selected from `otherArray` as replacements for the objects in `range`.
666+
public func replaceObjectsInRange(range: NSRange, withObjectsFromArray otherArray: [AnyObject], range otherRange: NSRange) {
667+
let slicedOtherArray = Array(otherArray[otherRange.location...otherRange.length+otherRange.location])
668+
replaceObjectsInRange(range, withObjectsFromArray: slicedOtherArray)
669+
}
670+
671+
/// Replaces the objects in the receiving array specified by a given range with all of the objects from a given array.
672+
/// - parameter range: The range of objects to be replaced in (or removed from) the receiving array.
673+
/// - parameter otherArray: The array of objects from which to select replacements for the objects in `range`.
662674
public func replaceObjectsInRange(range: NSRange, withObjectsFromArray otherArray: [AnyObject]) {
663675
if self.dynamicType === NSMutableArray.self {
664676
_storage.reserveCapacity(count - range.length + otherArray.count)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// This source file is part of the Swift.org open source project
2+
//
3+
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
4+
// Licensed under Apache License v2.0 with Runtime Library Exception
5+
//
6+
// See http://swift.org/LICENSE.txt for license information
7+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8+
//
9+
10+
11+
12+
#if DEPLOYMENT_RUNTIME_OBJC || os(Linux)
13+
import Foundation
14+
import XCTest
15+
#else
16+
import SwiftFoundation
17+
import SwiftXCTest
18+
#endif
19+
20+
21+
22+
class TestNSMutableArray : XCTestCase {
23+
24+
var allTests : [(String, () -> ())] {
25+
return [
26+
("test_replaceObjectsInRangeWithObjectsFromArray", test_replaceObjectsInRangeWithObjectsFromArray),
27+
]
28+
}
29+
30+
func test_replaceObjectsInRangeWithObjectsFromArray() {
31+
let existing = [NSNumber(int: 1), NSNumber(int: 2), NSNumber(int: 3), NSNumber(int: 4), NSNumber(int: 5), NSNumber(int: 6)]
32+
let nsExisting = NSMutableArray(array: existing)
33+
let otherArr = [NSNumber(int: 7), NSNumber(int: 8), NSNumber(int: 9)]
34+
let range = NSMakeRange(3, 3)
35+
nsExisting.replaceObjectsInRange(range, withObjectsFromArray: otherArr)
36+
XCTAssertEqual((nsExisting[0] as! NSNumber).intValue, 1)
37+
XCTAssertEqual((nsExisting[1] as! NSNumber).intValue, 2)
38+
XCTAssertEqual((nsExisting[2] as! NSNumber).intValue, 3)
39+
XCTAssertEqual((nsExisting[3] as! NSNumber).intValue, 7)
40+
XCTAssertEqual((nsExisting[4] as! NSNumber).intValue, 8)
41+
XCTAssertEqual((nsExisting[5] as! NSNumber).intValue, 9)
42+
}
43+
44+
func test_replaceObjectsInRangeWithObjectsFromArrayOtherRange() {
45+
let existing = [NSNumber(int: 1), NSNumber(int: 2), NSNumber(int: 3), NSNumber(int: 4), NSNumber(int: 5), NSNumber(int: 6)]
46+
let nsExisting = NSMutableArray(array: existing)
47+
let otherArr = [NSNumber(int: 7), NSNumber(int: 8), NSNumber(int: 9), NSNumber(int: 1), NSNumber(int: 2), NSNumber(int: 3)]
48+
let range = NSMakeRange(3, 3)
49+
let otherRange = NSMakeRange(2, 3)
50+
nsExisting.replaceObjectsInRange(range, withObjectsFromArray: otherArr, range: otherRange)
51+
XCTAssertEqual((nsExisting[0] as! NSNumber).intValue, 1)
52+
XCTAssertEqual((nsExisting[1] as! NSNumber).intValue, 2)
53+
XCTAssertEqual((nsExisting[2] as! NSNumber).intValue, 9)
54+
XCTAssertEqual((nsExisting[3] as! NSNumber).intValue, 1)
55+
XCTAssertEqual((nsExisting[4] as! NSNumber).intValue, 2)
56+
XCTAssertEqual((nsExisting[5] as! NSNumber).intValue, 6)
57+
}
58+
}

TestFoundation/main.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ XCTMain([
3636
TestNSIndexPath(),
3737
TestNSIndexSet(),
3838
TestNSJSONSerialization(),
39+
TestNSMutableArray(),
3940
TestNSNotificationCenter(),
4041
TestNSNumber(),
4142
TestNSPipe(),

0 commit comments

Comments
 (0)