NSArray
NSArray实例可以保存一组指向其他对象的指针。
17.1创建数组
NSDate *now = [NSDate date];
NSDate *tomorrow = [now dateByAddingTimeInterval:24.0*60.0*60.0];
NSDate *yesterday = [now dateByAddingTimeInterval:-24.0*60.0*60.0];
NSArray *dateList = @[now,tomorrow,yesterday];
NSArry实例一旦被创建后,就无法添加或删除数组里的指针,也无法改变数组的指针顺序。
17.2存取数组
NSDate *now = [NSDate date];
NSDate *tomorrow = [now dateByAddingTimeInterval:24.0*60.0*60.0];
NSDate *yesterday = [now dateByAddingTimeInterval:-24.0*60.0*60.0];
NSArray *dateList = @[now,tomorrow,yesterday];
NSLog(@"is %@",dateList[0]);
NSLog(@"there are %lu dates",[dateList count]);
遍历数组
NSDate *now = [NSDate date];
NSDate *tomorrow = [now dateByAddingTimeInterval:24.0*60.0*60.0];
NSDate *yesterday = [now dateByAddingTimeInterval:-24.0*60.0*60.0];
NSArray *dateList = @[now,tomorrow,yesterday];
for(int i=0;i<dateList.count;i++){
}
for(NSDate *d in dateList){
NSLog(@"Here is a date : %@",d);
}
NSMutableArray
可以添加删除或对指针重新进行排序。
NSDate *now = [NSDate date];
NSDate *tomorrow = [now dateByAddingTimeInterval:24.0*60.0*60.0];
NSDate *yesterday = [now dateByAddingTimeInterval:-24.0*60.0*60.0];
NSMutableArray *dateList = [NSMutableArray array];
[dateList addObject:now];
[dateList addObject:tomorrow];
[dateList insertObject:yesterday atIndex:0];
for(NSDate *d in dateList){
}
[dateList removeObjectAtIndex:0];
NSArray *dateList = [NSArray arrayWithObjects:now,tommorow,yesterday,nil];
[dateList objectAtIndex:0];
[dateList count];
NSArray *ne = @[now,tomorrow,yesterday];
NSDate *firstDate = dateList[0];