您的位置:首页 > 其它

Foundation框架学习2--数组、字典、集合

2014-05-10 21:27 411 查看
1.数组

// 数组
// 数组的初始化
NSArray *firstArray=[NSArray arrayWithObject:@"one"];
// 多个元素初始化时
NSArray *secondArray=[NSArray arrayWithObjects:@"one",@"two",@"three", nil];
[secondArray writeToFile:@"/Users/lizhensheng/test1" atomically:YES];
NSArray *threeArray=[NSArray arrayWithArray:secondArray];
// 从文件中保存的xml 数据初始化数组
NSArray *fourthArray=[NSArray arrayWithContentsOfFile:@"/Users/lizhensheng/test1"];
// 遍历数组
// 遍历性能高
for (NSString *str in fourthArray) {
NSLog(@"found %@",str);
}
// 数组中的常用方法
// 获取数组的元素个数
NSInteger count=[fourthArray count];
// 遍历性能低
for (int i=0; i<count; i++) {
NSLog(@"for 遍历:%@",fourthArray[i]);
}
// 获取数组下标中的 值
NSString *astr1=[fourthArray objectAtIndex:0];
NSString *astr2=fourthArray[0];

// 在原来的数组上加上对象,并付给新的数组
NSArray *addArray=[fourthArray arrayByAddingObject:@"fine"];
// 用指定的字符,将数组中的元素连接起来
NSString *arrStr=[fourthArray componentsJoinedByString:@","];
// 判断数组中是否包含某对象
BOOL isContain=[fourthArray containsObject:@"one"];
//查询指定对象在数组中的元素,如果没有此元素,返回NSNotFound
NSInteger index=[fourthArray indexOfObject:@"ccc"];
// 获取数组中最后一个元素
NSString *lastStr=[fourthArray lastObject];

// 可变数组
NSMutableArray *mutableArray=[NSMutableArray arrayWithCapacity:5];
// 向数组中添加元素
[mutableArray addObject:@"mutable1"];
// 向数组中添加数组
[mutableArray addObjectsFromArray:fourthArray];
// 向数组中指定下标插入元素
[mutableArray insertObject:@"bbb" atIndex:0];
// 移除最后一个元素
[mutableArray removeLastObject];
// 移除指定元素
[mutableArray removeObject:@"bbb"];
// 移除指定下标的元素
[mutableArray removeObjectAtIndex:0];
// 替换指定的下标元素
[mutableArray replaceObjectAtIndex:0 withObject:@"ccc"];
// 移除所有元素
[mutableArray removeAllObjects];


2.字典

// 字典
// 不可变字典
NSNumber *intNum=[NSNumber numberWithInt:100];
// 初始化一个元素  ,key value 必须是 对象
NSDictionary *dic1=[NSDictionary dictionaryWithObject:intNum forKey:@"key"];
// 初始化两个元素
NSDictionary *dic2=[NSDictionary dictionaryWithObjectsAndKeys:intNum,@"key1",intNum,@"key2", nil];
// 初始化字典包含其他字典
NSDictionary *dic3=[NSDictionary dictionaryWithDictionary:dic2];
// 把字典保存到文件中
[dic3 writeToFile:@"/Users/lizhensheng/test2" atomically:YES];
// 从文件中读取数据来初始化字典
NSDictionary *dic4=[NSDictionary dictionaryWithContentsOfFile:@"/Users/lizhensheng/test2"];

// 字典的常用方法
// 获取字典的 数量
NSInteger dicCount=[dic3 count];
// 通过key获取value
NSString *value=[dic3 objectForKey:@"key"];
// 将字典的key转换成一个枚举对象,用于遍历
NSEnumerator *enumerator=[dic3 keyEnumerator];
// 获取所有键的集合
NSArray *keys=[dic3 allKeys];
// 获取所有值的集合
NSArray *values=[dic3 allValues];

// 可变字典
// 初始化一个可变字典
NSMutableDictionary *mdic1=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"v1",@"k1",@"v2",@"k2", nil];
// 向字典中添加整个字典
[mdic1 addEntriesFromDictionary:dic3];
// 向字典中追加一个新的key和value
[mdic1 setObject:@"v4" forKey:@"k4"];
// 初始化一个空的字典
NSMutableDictionary *mdic2=[NSMutableDictionary dictionary];
// 将空字典mdic2对象内容设置与字典2对象相同
[mdic2 setDictionary:dic3];
// 将字典中k1 对应的值删除
[mdic2 removeObjectForKey:@"k1"];
NSArray *dicArr=[NSArray arrayWithObjects:@"k1",@"k2", nil];
// 根据字典中的key 移除字典中的值
[mdic2 removeObjectsForKeys:dicArr];
// 移除所有的内容
[mdic2 removeAllObjects];

// 遍历字典
// 快速遍历
for (id key in mdic1) {
id obj=[mdic1 objectForKey:key];
NSLog(@"快速遍历:key:%@  == value:%@",key,obj);
}
// 一般的枚举
NSArray *dkeys=[mdic1 allKeys];
NSUInteger dleng=[dkeys count];
for (int i=0; i<dleng; i++) {
id key=[dkeys objectAtIndex:i];
id obj=[mdic1 objectForKey:key];
NSLog(@"一般的枚举:key:%@  == value:%@",key,obj);
}
// 通过枚举类型枚举
NSEnumerator *denumerator=[mdic1 keyEnumerator];
id dkey=[denumerator nextObject];
while (dkey) {
id obj=[mdic1 objectForKey:dkey];
NSLog(@"枚举类型枚举:key:%@  == value:%@",dkey,obj);
dkey=[denumerator nextObject];
}


3.集合
// 集合
// 不可变集合
// 集合的初始化
NSSet *set1=[[NSSet alloc]initWithObjects:@"set1",@"set2",@"set3", nil];
NSArray *setArr=[NSArray arrayWithObjects:@"1",@"2",@"3", nil];
// 通过数组构建集合
NSSet *set2=[NSSet setWithArray:setArr];
// 通过已经有的集合构建集合
NSSet *set3=[[NSSet alloc]initWithSet:set2];

// 常用方法
NSUInteger setCount =[set3 count];
//以数组的形式返回集合3的元素
NSArray *set3Arr=[set3 allObjects];
// 返回集合3中的任意一个对象
id setObj=[set3 anyObject];
// 判断集合中是否包含内容为3的对象,如果包含返回YES,否则返回NO
BOOL isSetContain=[set3 containsObject:@"3"];
// 判断两个集合中是否有相同元素的对象,如果有返回YES,否则为NO
BOOL isIntersect=[set2 intersectsSet:set1];
// 两个集合中元素是否完全匹配,如果匹配返回YES,否则返回NO
BOOL isEqual=[set3 isEqualTo:set2];
// 集合3 是否是 集合2 的子集合,如果是返回YES,否则返回NO
BOOL isSub=[set3 isSubsetOfSet:set2];
// 在原来集合的基础上添加新的对象,并付给新的集合
NSSet *set4=[set2 setByAddingObject:@"4"];

// 可变集合
NSMutableSet *mset1=[NSMutableSet set];
NSMutableSet *mset2=[NSMutableSet setWithObjects:@"1",@"2", nil];
NSMutableSet *mset3=[NSMutableSet setWithObjects:@"a",@"2", nil];
// 集合2 减去 集合3中的元素,集合2最后元素只有1个,且为1
[mset2 minusSet:mset3];
// 集合2 与集合3中元素的交集,集合2最后元素只有1个,且为2
[mset2 intersectsSet:mset3];
// 集合2 与集合3 的并集,集合2最后有3个,且为1,2,a
[mset2 unionSet:mset3];
// 讲空集合1设为集合3的内容
[mset1 setSet:mset3];

for (id set in mset2) {
NSLog(@"1set:%@",set);
}
NSArray *setArray=@[@"3"];
// 从数组中添加元素到集合中
[mset2 addObjectsFromArray:setArray];
for (id set in mset2) {
NSLog(@"2set:%@",set);
}
// 根据内容删去集合中的对象
[mset2 removeObject:@"2"];
[mset2 removeAllObjects];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: