学习了一些NSArray的用法记录下。:lol
- //创建数组的类方法
- NSArray * myArray = [NSArray arrayWithObjects:@"aaaaa",@"mac",@"String", nil nil];
- NSArray * array1 = [NSArray arrayWithArray:myArray];
- /获取数组个数
- NSLog(@"myArray count :%d", [myArray count]);
- //追加对象,返回新的数组对象
- NSArray *array2 = [array1 arrayByAddingObject:@"gao"];
- NSLog(@"array2 :%@", array2);
- //是否包含指定对象
- NSLog(@"array2 baohan :%@", [array2 containsObject:@"gao"]);
- //从数组合并元素到字符串- componentsJoinedByString:
- NSArray *array = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil];
- NSString *string = [array componentsJoinedByString:@","];
- NSLog(@"string:%@",string);
- //字符串分割成数组
- NSString *string = [[NSString alloc] initWithString:@"One,Two,Three,Four"];
- NSLog(@"string:%@",string);
- NSArray *array = [string componentsSeparatedByString:@","];
- NSLog(@"array:%@",array);
- //快速遍历数组
- for (id element in array2) {
- NSLog(@"element :%@", element);
- }
复制代码
|
|