数组的批量操作
OC:
NSMutableArray *arr =
[NSMutableArray arrayWithObjects:@1, @2, @3, nil];
NSRange range = NSMakeRange(0, 2);
[arr replaceObjectsInRange:range
withObjectsFromArray:@[@99, @88, @77, @66]];
NSLog(@"%@", arr);
输出结果:
2016-04-01 14:56:45.493 OCTest[3599:667081] (
99,
88,
77,
66,
3
99,
88,
77,
66,
3
)
swift:
var arr = [1, 2, 3]
arr.replaceRange(1..<2, with: [99, 88, 77, 66])
arr.replaceRange(1..<2, with: [99, 88, 77, 66])
print(arr)
输出结果: [1, 99, 88, 77, 66, 3]
var arr = [1, 2, 3]
arr.replaceRange(Range(start: 0, end: 2), with: [99, 88, 77, 66])
arr.replaceRange(Range(start: 0, end: 2), with: [99, 88, 77, 66])
print(arr)
输出结果: [99, 88, 77, 66, 3]
遍历
OC:
NSArray *arr = @[@1, @2, @3];
for (int i = 0; i < arr.count; i++) {
NSLog(@"%@", arr[i]);
}
输出结果:
2016-04-01 15:00:10.702 OCTest[3635:694273] 1
2016-04-01 15:00:10.703 OCTest[3635:694273] 2
2016-04-01 15:00:10.703 OCTest[3635:694273] 2
2016-04-01 15:00:10.703 OCTest[3635:694273] 3
NSArray *arr = @[@1, @2, @3];
for (NSNumber *number in arr) {
NSLog(@"%@", number);
}
输出结果:
2016-04-01 15:03:43.192 OCTest[3684:723081] 1
2016-04-01 15:03:43.193 OCTest[3684:723081] 2
2016-04-01 15:03:43.193 OCTest[3684:723081] 2
2016-04-01 15:03:43.194 OCTest[3684:723081] 3
swift:
var arr1 = [1, 2, 3]
for var i = 0 ; i < arr1.count ; i++
{
print(arr1[i])
for var i = 0 ; i < arr1.count ; i++
{
print(arr1[i])
}
for number in arr1
{
print(number)
{
print(number)
}
输出结果:
1
2
2
3
var arr2 = [1, 2, 3]
for number in arr2[0..<3]
{
print(number)
for number in arr2[0..<3]
{
print(number)
}
输出结果:
1
2
2
3