您的位置:首页 > 其它

OC07_数组NSArray

2015-09-09 08:35 141 查看
(一).不可变数组
int main(int argc, const char * argv[]) {
1.数组:有序的集合
// NSArray
NSArray *arr1 = [NSArray arrayWithObjects:@"哈哈", @"abc", nil];
NSLog(@"数组信息:%@", arr1);




2.NSData
(1).将数组转化为二进制的数据.
NSData *data = [NSJSONSerialization dataWithJSONObject:arr1 option:NSJSONWritingPrettyPrinted error:nil];

(2).将二进制的数据转化为新的字符串,打印中文.
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"数组信息:%@“, str);




(3).字面量,用字面量创建一个对象都是不可变的.
NSArray *arr2 = @[@"First Blood", @"Double Kill"];
NSLog(@"数组信息:%@", arr2);




(4).数组的元素个数.
NSLog(@"数组的元素个数为:%lu", arr2.count);




(5).取出数组的元素.
arr[下标] == [arr objectAtIndex:下标];
(1).
NSString *str2 = [arr2 objectAtIndex:0];
NSLog(@"%@", str2);




(2).
NSLog(@"%@", arr2[0]);




3.数组遍历
(1).
for (NSInteger i = 0 ; i < arr2.count; i++) {
NSLog(@"*%@*", [arr2 objectAtIndex:i]);
}




(2).
for (NSInteger i = 0; i < arr2.count; i++) {
NSLog(@"**%@**", arr2[i]);
}




(3).
for in 快速枚举器 Fast Enumeration
type * 容器中的元素类型.
object 操作使用的临时对象名
collection 数据容器.
for(NSString *temp in arr2) {
NSLog(@"***%@***", temp);
}




4.包含对象
NSString *str0 = @"First Blood";
if([arr2 containsObject:str0]) {
NSLog(@"数组包含该对象");
} else {
NSLog(@"数组不包含该对象");
}




5.根据对象获取下标.
NSLog(@"index:%lu", [arr2 indexOfObject:@"Double Kill"])




6.获取第一个元素/最有一个元素.
NSLog(@"%@", arr2.firstObject);
NSLog(@"%@", [arr2 lastObject]);




(二).可变数组.
// NSMutableArray
(1).
NSMutableArray *nsMA = [NSMutableArray arrayWithCapacity:0];
(2).
NSMutableArray *nsMA = [[NSMutableArray alloc] initWithCapacity:0];

(3).打印数组的类型.
NSArray *nsA = [NSArray array];
NSLog(@"%@ %@", nsMA.class, nsA.class);




(4).添加元素.
NSMutableArray *nsMA1 = [NSMutableArray array];
[nsMA addObject:@"Top"];
[nsMA addObject:@"Jungle"];
[nsMA addObject:@"Mid"];

[nsMA1 addObject:@"ABC"];
[nsMA1 addObject:@"Support"];




(5).添加数组对象(把数组作为整体进行添加).
[nsMA addObject:nsMA1];
NSLog(@"%@", nsMA);




(6).从一个数组中添加元素.
[nsMA addObjectsFromArray:nsMA1];
NSLog(@"%@", nsMA);




(7).插入 insert.
[nsMA insertObject:@"Fo od" atIndex:1];   // 打印结果会出现双引号,因为有空格.
NSLog(@"%@", nsMA);




(8).移除 remove.
[nsMA removeObject:@"Fo od"];
[nsMA removeObjectAtIndex:1];
NSLog(@"%@", nsMA);




(9).替换 replace.
[nsMA replaceObjectAtIndex:1 withObject:@"Run"];
NSLog(@"%@", nsMA);




(10).交换 exchange.
[nsMA exchangeObjectAtIndex:2 withObjectAtIndex:3];
NSLog(@"%@", nsMA);




(三).数对象
(1).NSNumber 数对象
int -> number
NSNumber *intNumber = [NSNumber numberWithInt:100];

number -> int
int intValue = intNumber.intValue;
NSLog(@"%@, %d", intNumber, intVal);




(2).字面量
NSNumber *literal =@100;
NSLog(@"%@, %ld", literal, (long)literal.integerValue);




number -> string
NSLog(@"%@", literal.stringValue);




(四).值对象
NSValue
NSRange r = NSMakeRange(3, 4);
(1). range -> value
NSValue *value = [NSValue valueWithRange:r];
NSLog(@"%@, %ld", value, r.location);




(2). value ->range
NSRange R = value.rangeValue;
NSLog(@"%lu %lu", R.location, R.length);




NSNumber 转化的基础类型.
NSValue 转化的结构体和指针.


return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: