标签:
数组的概念和常用方法:
Foundation中的数组(NSArray,NSMutableArray)是一组有序的对象集合,通过索引下标获取数组中各个元素。与字符串相同,数组也有可变和不可变数组之分。
数组中不可以存放基本数据类型,只能存放类的对象。
如果需要将基本数据类型放入数组中,需要通过NSNumber进行数据的“封装“。
//数组的创建与初始化
//(1)调用系统创建方法(系统创建的方法已经加了autorelease),不需要release
NSArray *array = [NSArray array];
NSLog(@"%@", array);
//用单个元素初始化数组
NSArray *firstArray = [NSArray arrayWithObject:@"one"];
NSLog(@"%@", firstArray);
NSArray *firstArray2 = @[@"one"];
NSLog(@"%@", firstArray2);
//用多个元素初始化时,注意以nil作为数组的结束
NSArray *secondArray = [NSArray arrayWithObjects:@"one",@"two", nil];
NSLog(@"%@", secondArray);
NSArray *secondArray2 = @[@"one", @"two"];
NSLog(@"%@", secondArray2);
//用数组初始化
NSArray *thirdArray = [NSArray arrayWithArray:secondArray];
NSLog(@"%@", thirdArray);
// 用文件初始化
//NSArray *FourthArray = [NSArray arrayWithContentsOfFile:@"test.plist"]; // 与文件有关 后续讲解
//(2)自己alloc的对象并初始化,需要release或autorelease
//声明一个空数组
NSArray *ary1 = [[[NSArray alloc] init] autorelease];
NSLog(@"%@", ary1);
//用一个数组初始化另一个数组
NSArray *ary2 = [[[NSArray alloc] initWithArray:ary1] autorelease];
NSLog(@"%@", ary2);
//用一组对象初始化
NSArray *ary3 = [[[NSArray alloc] initWithObjects:@"hello", @"world", @"student", nil] autorelease];
NSLog(@"%@", ary3);
//与文件有关的后续讲解
// NSArray *ary4 = [[[NSArray alloc] initWithContentsOfFile:@"test.plist"] autorelease];
NSArray *array = [NSArray arrayWithObjects:@"a",@"b",@"c", nil];
// 获取数组中的元素个数
NSInteger count = [array count];
NSLog(@"总个数%d", count);
// 根据下标访问对象
NSString *firstObj = [array objectAtIndex:0];
firstObj = array[0];
NSLog(@"%@", firstObj);
// 在原来的数组上追加对象,返回一个新的数组对象
NSArray *addArray = [array arrayByAddingObject:@"ddd"];
NSLog(@"%@", addArray);
// 用指定的字符串将数组中的元素连接起来
NSString *str = [array componentsJoinedByString:@","];
NSLog(@"%@", str);
// 数组中是否包含某对象
BOOL isContain = [array containsObject:@"bbb"];
NSLog(@"%d", isContain);
// 获取指定对象在数组中的索引, 如果没此元素,返回NSNotFound
NSInteger index = [array indexOfObject:@"ccc"];
NSLog(@"%d", index);
// 获取数组中最后?个元素
NSString *lastString = [array lastObject];
NSLog(@"%@", lastString);
//获取数组中第一个元素
NSString *firstString = [array firstObject];
NSLog(@"%@", firstString);
//向一个数组中添加另一个数组的所有元素
NSArray *newArray = [array arrayByAddingObjectsFromArray:addArray];
NSLog(@"%@", newArray);
//可变数组
NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:5]; //初始化
//或者
mutableArray = [[NSMutableArray alloc] initWithCapacity:5];//初始化
// 向数组中添加元素
[mutableArray addObject:@"akgo1"];
[mutableArray addObject:@"akgo2"];
[mutableArray addObject:@"akgo3"];
[mutableArray addObject:@"akgo4"];
NSLog(@"%@", mutableArray);
// 向数组中指定下标插?元素
[mutableArray insertObject:@"xcode" atIndex:0];
NSLog(@"%@", mutableArray);
// 移除最后一个元素
[mutableArray removeLastObject];
NSLog(@"%@", mutableArray);
// 移除指定元素
[mutableArray removeObject:@"swift"];
NSLog(@"%@", mutableArray);
// 移除指定下标的元素
[mutableArray removeObjectAtIndex:0];
NSLog(@"%@", mutableArray);
//移除指定范围内的元素
[mutableArray removeObjectsInRange:NSMakeRange(0, 2)];
NSLog(@"%@", mutableArray);
// 移除所有对象
[mutableArray removeAllObjects];
NSLog(@"%@", mutableArray);
// 向数组中添加另一数组的所有元素
NSArray *array = [NSArray arrayWithObjects:@"a",@"b",@"c", nil];
[mutableArray addObjectsFromArray:array];
NSLog(@"%@", mutableArray);
// 替换指定的下标元素
[mutableArray replaceObjectAtIndex:0 withObject:@"replace"];
NSLog(@"%@", mutableArray);
//交换指定所索的两个元素
[mutableArray exchangeObjectAtIndex:0 withObjectAtIndex:1];
NSLog(@"%@", mutableArray);
NSArray *array = [NSArray arrayWithObjects:@"HELLO", @"friend", @"thanks", nil];
int length = [array count];
//一般遍历,遍历性能较低
for (int i = 0; i < length; i++)
{
NSString *element = [array objectAtIndex:i];
NSLog(@"%@", element);
}
//快速枚举,遍历的性能较高
for (NSString *str in array)
{
NSLog(@"%@", str);
}
// 当不确定数组的元素的类型时,可以选择用id
for (id str in array)
{
NSLog(@"%@", str);
}
/数组排序
//@"41", @"23", @"3", @"45", @"6", @"100", nil
//@"hello", @"world", @"thanks", @"good", @"boy", @"list", nil
//利用可变数组排序
NSMutableArray *mutableAry = [NSMutableArray arrayWithObjects:@"41", @"23", @"3", @"45", @"6", @"100", nil];
//排序方式1(调用NSMutableArray的sortUsingFunction:方法和自定义的比较方法)
[mutableAry sortUsingFunction:compare context:nil];//从小到大排序
for (NSString *num in mutableAry)
{
NSLog(@"%@", num);
}
return;
//排序方式2(调用NSMutableArray的sortUsingComparator:方法和自定义的block语句块)
[mutableAry sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
//从小到大排序
if ([obj1 integerValue] > [obj2 integerValue])
{
return 1;
}
else if ([obj1 integerValue] < [obj2 integerValue])
{
return -1;
}
return 0;
//NSString *str1 = (NSString*)obj1;
//NSString *str2 = (NSString*)obj2;
//return [str1 compare:str2];
}];
for (NSString *num in mutableAry)
{
NSLog(@"%@", num);
}
return;
//利用不可变数组排序
NSArray *tmpAry = [NSArray arrayWithObjects:@"41", @"23", @"3", @"45", @"6", @"100", nil];
//排序方式3(调用NSArray的sortedArrayUsingFunction:方法和自定义的比较方法)
tmpAry = [tmpAry sortedArrayUsingFunction:compare context:nil];
for (NSString *num in tmpAry)
{
NSLog(@"%@", num);
}
return;
//排序方式4(调用NSArray的sortedArrayUsingComparator:和自定义的block语句块)
tmpAry = [tmpAry sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
//从小到大排序
if ([obj1 integerValue] > [obj2 integerValue])
{
return 1;
}
else if ([obj1 integerValue] < [obj2 integerValue])
{
return -1;
}
return 0;
//NSString *str1 = (NSString*)obj1;
//NSString *str2 = (NSString*)obj2;
//return [str1 compare:str2];
}];
for (NSString *num in tmpAry)
{
NSLog(@"%@", num);
}
字典基本概念和常用方法:
Foundation中的字典(NSDictionary,NSMutableDictionary)是由键-值对组成的数据集合。
通过key(键),查找对应的value(值),key通常是字符串对象,也可以是其他类型对象。
在一个字典对象中,key的值必须是唯一的。此外,字典对象的键和值不可以为空(nil)。
//字典的初始化
//(1)调用系统创建方法(系统创建的方法已经加了autorelease),不需要release
//创建一个空字典
NSDictionary *dic = [NSDictionary dictionary];
NSLog(@"%@", dic);
// 用一个元素初始化
NSNumber *numObj = [NSNumber numberWithInt:100];
NSDictionary *dic2 = [NSDictionary dictionaryWithObject:numObj forKey:@"key"];
//或者
NSDictionary *dic2_1 = @{@"key":numObj};
NSLog(@"%@ %@", dic2, dic2_1);
// 用多个元素初始化
NSNumber *numObj2 = [NSNumber numberWithInt:100];
NSDictionary *dic3 =[NSDictionary dictionaryWithObjectsAndKeys:numObj,@"key1",numObj2,@"key2", nil];
//或者
NSDictionary *dic3_1 = @{@"key1":numObj, @"key2":numObj2};
NSLog(@"%@ %@", dic3, dic3_1);
// 用一个字典初始化
NSDictionary *otherDic = [NSDictionary dictionaryWithObject:@"hello" forKey:@"H"];
NSDictionary *dic4 = [NSDictionary dictionaryWithDictionary:otherDic];
NSLog(@"%@", dic4);
//用文件内容初始化字典,与文件有关 后续讲解
// NSDictionary *dic5 = [NSDictionary dictionaryWithContentsOfFile:@"test.plist"];
//(2)自己alloc的对象并初始化,需要release或autorelease
NSDictionary *dic6 = [[NSDictionary alloc] init];
NSLog(@"%@", dic6);
NSDictionary *dic7 = [[NSDictionary alloc] initWithObjectsAndKeys:
@"value2", @"key1", @"value2", @"key2", nil];
NSLog(@"%@", dic7);
NSDictionary *dic8 = [[NSDictionary alloc] initWithDictionary:otherDic];
NSLog(@"%@", dic8);
// NSDictionary *dic9 = [[NSDictionary alloc] initWithContentsOfFile:@"test.plist"];
NSDictionary *dic =[NSDictionary dictionaryWithObjectsAndKeys:@"hello", @"key", @"thanks",
@"key2", @"xcode", @"key3", @"love", @"key4", nil];
//获取字典数量
NSInteger count = [dic count];
NSLog(@"总个数%d", count);
//通过key获取对应的value对象
NSString *valueObj = [dic objectForKey:@"key"];
NSLog(@"%@", valueObj);
//通过value获取对应的key数组(字典中允许有相同的value,但不允许有相同的key)
NSArray *keyAry = [dic allKeysForObject:valueObj];
for (NSString *key in keyAry)
{
NSLog(@"%@", key);
}
//获取所有键的集合
NSArray *keys = [dic allKeys];
NSLog(@"%@", keys);
//获取所有值的集合
NSArray *values = [dic allValues];
NSLog(@"%@", values);
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"v1", @"key1",@"v2", @"key2",
@"v3", @"key3", nil];
//快速遍历
for (NSString *key in dic)
{
id obj = [dic objectForKey:key];
NSLog(@"%@", obj);
}
//一般遍历
NSArray *keys = [dic allKeys];
for (int i = 0; i < [keys count]; i++)
{
id key = [keys objectAtIndex:i];
id obj = [dic objectForKey:key];
NSLog(@"%@", obj);
//字典排序方法
NSDictionary *myDic = [NSDictionary dictionaryWithObjectsAndKeys:@"hello", @"0", @"xcode", @"1", @"thanks",
@"3", @"world", @"4", @"room", @"5", nil];
//排序字典里的值value,返回有序值value对应的key数组
NSArray *aryKey = [myDic keysSortedByValueUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSString *str1 = (NSString*)obj1;
NSString *str2 = (NSString*)obj2;
return [str1 compare:str2];//从小到大排序
}];
for (NSString *key in aryKey)
{
NSLog(@"value=%@, key=%@", [myDic objectForKey:key], key);
}
// 初始化一个空的可变字典
NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
NSLog(@"%@", dic1);
// 初始化?个可变字典
NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"v1", @"key1", @"v2", @"key2", nil];
NSLog(@"%@", dic2);
//修改字典2中对象
[dic2 setObject:@"哈喽" forKey:@"key1"];
NSLog(@"%@", dic2);
// 向字典2对象中追加一个新的key3和value3
[dic2 setObject:@"value3" forKey:@"key3"];
NSLog(@"%@", dic2);
// 初始化?个不可变字典,并向字典2中追加该字典
NSDictionary *dic3 = [NSDictionary dictionaryWithObject:@"v3" forKey:@"key3"];
[dic2 addEntriesFromDictionary:dic3];
NSLog(@"%@", dic2);
// 将字典中key1对应的值删除
[dic2 removeObjectForKey:@"key1"];
NSLog(@"%@", dic2);
// 根据指定的数组(key2, key3)移除字典1的内容
NSArray *array = [NSArray arrayWithObjects:@"key2", @"key3", nil];
[dic2 removeObjectsForKeys:array];
NSLog(@"%@", dic2);
// 移除字典所有对象
[dic2 removeAllObjects];
NSLog(@"%@", dic2);
标签:
原文地址:http://blog.csdn.net/qq_34798977/article/details/51371557