NSArray
NSArray實例可以保存一組指向其他對象的指針。
17.1創(chuàng)建數(shù)組
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實例一旦被創(chuàng)建后誉简,就無法添加或刪除數(shù)組里的指針碉就,也無法改變數(shù)組的指針順序。
17.2存取數(shù)組
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]);
遍歷數(shù)組
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
可以添加刪除或?qū)χ羔樦匦逻M(jìn)行排序闷串。
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];