//数组存放的内容必须是NSOject对象
//数组存放的内容是有序的
/**
* NSArray 不可变数组
*/
/**
* 1.创建对象
*/
//使用初始化方法
NSArray *array1 = [[NSArray alloc]initWithObjects:@10, @"娜娜", @"怀玉", @"满明强", @"金明", @"阿福", nil];
NSArray *array2 = [[NSArray alloc]initWithArray:array1];
//类方法
NSArray *array3 = [NSArray arrayWithObjects:@"曲国伟", @"小南", @"周雨琪", @"bobo", @"xiaozun", nil];
//字面量方法
NSArray *array4 = @[@"yangyunda", @"yangshukai", @"yida", @"wumingwei"];
NSLog(@"array4 = %@", array4);
/**
* 2.数组查找
*/
//通过下标访问对象
NSLog(@"%@", array1[1]);
NSString *str1 = [array1 objectAtIndex:2];
NSLog(@"%@", str1);
//通过对象访问下标
NSString *str2 = @"阿福";
NSInteger index = [array1 indexOfObject:str2];
NSLog(@"%lu", index);
/**
* 3.判断数组中是否包含某个指定的对象
*/
BOOL isHave = [array3 containsObject:@"小南"];
NSLog(@"%d", isHave);
/**
* 4.NSString和NSArray之间的转换
*/
NSString *string1 = @"孙超&程晗&孟玲旭";
//将一个字符串按照指定字符截取, 每段内容存放在数组中
NSArray *strArray = [string1 componentsSeparatedByString:@"&"];
NSLog(@"%@ %@ %@", strArray[0], strArray[1], strArray[2]);
//将数组中的元素按照指定的字符串拼接成新的字符串对象
NSArray *array5 = @[@"雷坤", @"于艳萍", @"尹浩"];
NSString *string2 = [array5 componentsJoinedByString:@"&"];
NSLog(@"%@", string2);
//练习: 将字符串转为数组
NSString *strTemp = @"http://www.imanhua.com/Cover/2011-10/hyrz.jpg&http://www.imanhua.com/Cover/2011-09/op.jpg&http://www.imanhua.com/Cover/2012-04/yjdwb.jpg";
NSArray *tempArray = [strTemp componentsSeparatedByString:@"&"];
NSLog(@"%@", tempArray);
/**
* NSMutableArray 可变的数组
*/
//1.创建对象
//初始化方法
NSMutableArray *mArray = [[NSMutableArray alloc]initWithObjects:@10, @4.3, @"dfa", @"qewfk", nil];
//类方法
NSMutableArray *mArray1 = [NSMutableArray arrayWithObjects:@"阿斯蒂芬", @43, @54.43, @"大师傅", nil];
//2.添加元素 插入元素
[mArray addObject:@"王"];
NSLog(@"%@", mArray);
[mArray insertObject:@10 atIndex:1];
NSLog(@"%@", mArray);
//3.删除元素
[mArray1 removeObject:@"阿斯蒂芬"];
NSLog(@"%@", mArray1);
//4.替换指定位置的对象
[mArray1 replaceObjectAtIndex:1 withObject:@"wuse"];
NSLog(@"%@", mArray1);
//5.交换两个指定位置的对象
[mArray1 exchangeObjectAtIndex:1 withObjectAtIndex:2];
NSLog(@"%@", mArray1);
/**
* 字典 存放键值对类型的数据
*/
/*
name : zhangsan
sex : boy
age : 13
*/
NSDictionary 不可变的字典
字典中存放的数据是无序的
字典中key值是唯一的
/**
* 1.创建对象
*/
初始化方法
NSDictionary *dict1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"zhangsan", @"name", @"boy", @"sex", @18, @"age", nil];
NSLog(@"%@", dict1);
NSDictionary *dict2 = [[NSDictionary alloc]initWithObjects:@[@"zhangsan", @"boy", @18] forKeys:@[@"name", @"sex", @"age"]];
NSLog(@"%@", dict2);
//类方法
NSDictionary *dict3 = [NSDictionary dictionaryWithObjectsAndKeys:@"liuaoran", @"name", @"nan", @"sex", @18, @"age", nil];
NSLog(@"%@", dict3);
//字面量方法
NSDictionary *dict4 = @{
@"name" : @"gaojie",
@"sex" : @"boy",
@"age" : @28,
@"hobby": @"nana"
};
NSLog(@"%@", dict4);
/**
* 2.查找
*/
//根据key查找object
NSString *string1 = [dict4 objectForKey:@"name"];
NSLog(@"%@", string1);
//下标法, key值作为下标
NSLog(@"%@", dict4[@"name"]);
/**
* 3.获取字典长度
*/
NSLog(@"%lu", [dict4 count]);
//NSMutableDictionary 可变的字典
//1.创建对象
NSMutableDictionary *mdic = [[NSMutableDictionary alloc]initWithDictionary:@{
@"name" : @"gaojie",
@"sex" : @"boy",
@"age" : @28,
@"hobby": @"nana"
}];
NSLog(@"%@", mdic);
NSMutableDictionary *mdic1 = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"wangqiang", @"name", @"nan", @"gender", @18, @"age", nil];
NSLog(@"%@", mdic1);
NSMutableDictionary *mdic2 = [[NSMutableDictionary alloc]initWithObjects:@[@"wangqiang", @"nan", @18]forKeys:@[@"name", @"gender", @"age"]];
NSLog(@"%@", mdic2);
NSMutableDictionary *mdic3 = [NSMutableDictionary dictionaryWithObject:@[@"zhangsan", @"boy", @18] forKey:@[@"name", @"sex", @"age"]];
NSLog(@"%@", mdic3);
//2.添加,修改键值对
//如果key值存在,修改key对应的object数据
//如果key值不存在,添加键值对
[mdic addEntriesFromDictionary:@{@"address" : @"qinghe"}];
NSLog(@"%@", mdic);
[mdic1 setObject:@14 forKey:@"class"];
NSLog(@"%@", mdic1);
[mdic1 setValue:@"清河大厦" forKey:@"address"];
NSLog(@"%@", mdic1);
[mdic1 setValue:@"金五星" forKey:@"address"];
NSLog(@"%@", mdic1);
/*
setValue: forKey: 的key参数类型必须是NSString *
setObject: forKey: 的key参数类型可以是任意类型的对象
setValue: forKey: 是NSObject对象的方法, 也就是说所有的OC对象都有这个方法
setObject: forKey: 是NSMutableDictionary类中特有的方法
*/
//3.删除
[mdic1 removeObjectForKey:@"address"];
NSLog(@"%@", mdic1);
//删除所有内容
[mdic1 removeAllObjects];
NSLog(@"%@", mdic1);
//NSSet 集合(元素不可以重复,无序存放)
//创建一个set对象
NSSet *set = [NSSet setWithObjects:@"liuyang", @"wangzhiting", @"wangqiang", @"yvejianhua", @"liuyang", nil];
NSLog(@"%@", set);
//任意取出一个元素
NSString *str = [set anyObject];
NSLog(@"str = %@", str);
//取出所有的元素
NSArray *array = [set allObjects];
NSLog(@"%@", array);
//获取集合中元素的个数
NSLog(@"%lu", set.count);
//判断对象是否在集合中
BOOL isHave = [set containsObject:@"liuyang"];
NSLog(@"%d", isHave);
//NSMutableSet 可变的集合
//创建对象
NSMutableSet *mset = [NSMutableSet setWithObjects:@"zhangmingjie", @"wangzhizhun", nil];
//添加对象
[mset addObject:@"-1"];
NSLog(@"%@", mset);
//删除对象
[mset removeObject:@"-1"];
NSLog(@"%@", mset);