需要标记参数数组的结束nil,实际不会存在数组中
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
int i;
@autoreleasepool { //创建数组方法,索引数从0开始
NSArray *monthNames = [NSArray arrayWithObjects: @"January", @"Febuary", @"March", @"April", @"May", @"June", @"July", @"August", @"September", @"October", @"November", @"December", nil];//nil实际并不会存储在数组中
//创建一个数组包含月份的名称
//列出数组中的所有元素
NSLog(@"Month Names");
NSLog(@"===== =====");
for( i = 0;i < 12;i++){
NSLog(@" %2i %@", i + 1, [monthNames objectAtIndex: i]);
//objectAtIndex:方法用索引数检索数组中的元素
}
}
return 0;
}
//使用一下语法也可以创建NSArray对象,@[elem1, elem2, ..., elemn];这种情况下不用将nil放在这些元素列表末尾
//array[index];相当于[array objectAtIndex:index];
//还可以使用这种语法将对象的引用存储到数组中:array[index] = object;这相当于[array setObject:object forIndex: index];
oc之数组对象学习
最新推荐文章于 2019-04-22 10:17:09 发布