OC集合类型
1.数组
2.字典
3.集合
NSArray, 不可变数组类, 用于存放一组有序的对象, 继承于NSObject
注: 数组中的元素必须是对象
创建数组对象
NSArray *array1 = [[NSArray alloc]initWithObjects:@"liudehua", @"zhangxueyou", @"guofucheng", @"liming", nil];
注: 元素之间用逗号隔开, 最后以nil结束
NSLog(@"%@", array1);//打印对象使用%@ NSArray *array2 = [NSArray arrayWithObjects:@"xiaoshengyang", @"songxiaobao", @"zhaosi", @"liuneng", nil]; NSLog(@"%@", array2);
数组的元素个数
NSUInteger count = [array2 count]; NSLog(@"%lu", count);
获取数组的某个元素
NSString *name = [array2 objectAtIndex:3]; NSLog(@"%@", name);
获取数组的最后一个元素
NSString *lastObject = [array2 lastObject]; NSLog(@"%@", lastObject);
获取数组的第一个元素
NSString *firstObject = [array2 firstObject]; NSLog(@"%@", firstObject);
遍历数组的每一个元素, 输出每一个元素的值
for (NSInteger i = 0; i < [array2 count]; i++) { NSLog(@"%@", [array2 objectAtIndex:i]); }
NSMutableArray, 可变数组类, 继承于NSArray
NSMutableArray *mArray = [[NSMutableArray alloc]initWithCapacity:0]; NSLog(@"%@", mArray);
添加元素
[mArray addObject:@"iPhone"]; [mArray addObject:@"iPad"]; [mArray addObject:@"Apple Watch"]; NSLog(@"%@", mArray);
注: 添加到数组的最后
插入元素
[mArray insertObject:@"iMac" atIndex:1]; NSLog(@"%@", mArray);
添加多个元素
NSArray *tempArray = [NSArray arrayWithObjects:@"iPhoto", @"iWork", @"iTunes", @"iBooks",nil]; [mArray addObjectsFromArray:tempArray]; NSLog(@"%@", mArray);
注: 数组元素必须是对象, 但是对象的数据局类型可以不相同
NSMutableArray *mArray2 = [NSMutableArray arrayWithObjects:@"A程序员", @"B程序媛", @"C设计狮", @"D产品狗", @"E运营鼠", @"F销售猴", @"G总经狸", nil]; NSLog(@"%@", mArray2);
移除某个元素
[mArray2 removeObjectAtIndex:1]; NSLog(@"%@", mArray2);
交换两个元素
[mArray2 exchangeObjectAtIndex:2 withObjectAtIndex:5]; NSLog(@"%@", mArray2);
替换某个元素
[mArray2 replaceObjectAtIndex:0 withObject:@"H工程狮"]; NSLog(@"%@", mArray2);
移除所有元素
[mArray2 removeAllObjects]; NSLog(@"%@", mArray2);
冒泡排序
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"iOS", @"Android", @"window phone", @"symbian", @"blackberry", nil]; NSLog(@"比较前:"); for (NSInteger i = 0; i < [array count]; i++) { NSString *a = [array objectAtIndex:i]; NSLog(@"%@", a); } for (NSInteger i = 0; i < [array count] - 1; i++) { for (NSInteger j = 0; j < [array count] - 1 - i; j++) { if ([[array objectAtIndex:j]compare:[array objectAtIndex:j + 1]] == NSOrderedDescending) { [array exchangeObjectAtIndex:j withObjectAtIndex:j + 1];//交换元素 } } } NSLog(@"\n比较后:"); for (NSInteger i = 0; i < [array count]; i++) { NSString *a = [array objectAtIndex:i]; NSLog(@"%@", a); }
for...in快速枚举
不需要关心下标和循环次数, 内部自动做处理
每一次循环, 把当前数组中的元素赋值给string
for (NSString *string in array) { NSLog(@"%@", string); }
数组的排序方法
OC中对数组进行排序, 只要给相邻两个元素的比较方法, 就会自动对数组进行排序, 并把排序的数组返回来
NSArray *sortArray = [array sortedArrayUsingSelector:@selector(compare:)];//caseInsensitiveCompare: NSLog(@"%@", sortArray);
把基本的数据类型转化为对象
int num1 = 1; float num2 = 10.10; double num3 = 20.123;
常用的方式
1.基本数据类型 -> NSString
NSString *str1 = [NSString stringWithFormat:@"%d", num1]; NSString *str2 = [NSString stringWithFormat:@"%f", num2]; NSString *str3 = [NSString stringWithFormat:@"%lf", num3]; // NSLog(@"%@ %@ %@", str1, str2, str3);
NSArray *numberArray = [NSArray arrayWithObjects:str1, str2, str3, nil]; NSLog(@"%@", numberArray); int num4 = [[numberArray objectAtIndex:0] intValue]; float num5 = [[numberArray objectAtIndex:1] floatValue];//转化为浮点类型 double num6 = [[numberArray objectAtIndex:2] doubleValue]; NSLog(@"%d %f %lf", num4, num5, num6);
2.基本数据类型 -> NSNumber
NSNumber, 数值类, 把基本的数据类型(int, short, char, long, float, double, BOOL)转换成NSNumber的对象, 或者把NSNumber的对象转换成基本数据类型, 继承于NSValue
//int -> number NSNumber *intNumber = [NSNumber numberWithInt:10]; //float -> number NSNumber *floatNumber = [NSNumber numberWithFloat:3.14]; //BOOl -> number NSNumber *boolNumber = [NSNumber numberWithBool:YES]; NSArray *numberArray1 = [NSArray arrayWithObjects:intNumber, floatNumber, boolNumber, nil]; NSLog(@"%@", numberArray1); //number -> int int num7 = [intNumber intValue]; //number -> float float num8 = [floatNumber floatValue]; //number -> BOOL BOOL num9 = [boolNumber boolValue]; NSLog(@"%d %f %d", num7, num8, num9);
字典
NSDictionary, 不可变字典类, 继承于NSObject, 字典中存放的数据以键值对(key-value)形式存在, key值是唯一的, 不能重复
创建字典对象
NSDictionary *dic1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"xiaoming", @"name", @"male", @"gender", @"18", @"age", nil]; NSLog(@"%@", dic1);
注: 1.字典是无序的
2.value在前, key在后
3.以nil结尾
如何获取value值?
通过key值获取value值
NSString *nameString = [dic1 objectForKey:@"name"]; NSLog(@"%@", nameString); NSString *genderString = [dic1 objectForKey:@"gender"]; NSLog(@"%@", genderString); NSString *ageString = [dic1 objectForKey:@"age"]; NSLog(@"%@", ageString);
获取所有的value值
NSArray *values = [dic1 allValues]; NSLog(@"%@", values);
获取所有的key值
NSLog(@"%@", [dic1 allKeys]);
求字典的键值对个数
NSLog(@"%lu", [dic1 count]);
遍历字典的键值对, 输出value和key值
//1 for (NSInteger i = 0; i < [[dic1 allKeys] count]; i++) { NSString *key = [[dic1 allKeys] objectAtIndex:i]; NSString *value = [dic1 objectForKey:key]; NSLog(@"%@ - %@", key, value); } //2 for (NSString *key in [dic1 allKeys]) { NSLog(@"%@ - %@", key, [dic1 objectForKey:key]); } //3 for...in对字典使用, 遍历的是key值 for (NSString *string in dic1) { NSLog(@"%@ - %@", string, [dic1 objectForKey:string]); }
NSMutableDictionary, 可变字典类, 继承于NSDictionary
NSMutableDictionary *mDic = [[NSMutableDictionary alloc]initWithCapacity:0]; NSLog(@"%@", mDic);
添加键值对(key值不存在)
[mDic setObject:@"bianda" forKey:@"dawa"]; [mDic setObject:@"qianliyan" forKey:@"erwa"]; [mDic setObject:@"daoqiangburu" forKey:@"sanwa"]; NSLog(@"%@", mDic);
修改键值对(key值已存在)
[mDic setObject:@"lidawuqiong" forKey:@"dawa"]; NSLog(@"%@", mDic);
删除键值对
[mDic removeObjectForKey:@"sanwa"]; NSLog(@"%@", mDic);
删除所有
[mDic removeAllObjects]; NSLog(@"%@", mDic);
集合
NSSet, 不可变集合, 集合内的元素都是无序的, 元素唯一, 继承于NSObject
NSSet *set = [[NSSet alloc] initWithObjects:@"1", @"2", @"3", nil]; NSLog(@"%@", set);
获取集合元素个数
NSLog(@"%lu", [set count]);
访问集合中的元素
NSLog(@"%@", [set anyObject]);
访问集合中所有的元素
NSLog(@"%@", [set allObjects]);
将数组重复值取消
NSArray *arr = [NSArray arrayWithObjects:@"1", @"1", @"2", @"2", @"3", @"3", nil]; NSLog(@"%@", arr); NSSet *set1 = [NSSet setWithArray:arr]; NSLog(@"%@", [set1 allObjects]);//数组输出 NSSet *set11 = [NSSet setWithObjects:@"2", @"3", nil]; NSSet *set12 = [NSSet setWithObjects:@"4", @"2", @"3", nil];
判断集合是否包含另外一个集合
if ([set11 isSubsetOfSet:set12]) { NSLog(@"是子集合"); } //相交 if ([set11 intersectsSet:set12]) { NSLog(@"相交"); } //相等 if ([set11 isEqualToSet:set12]) { NSLog(@"相等"); }
NSMutableSet, 可变集合, 继承于NSSet
NSMutableSet *mSet = [[NSMutableSet alloc] initWithCapacity:0]; NSLog(@"%@", mSet);
添加元素
[mSet addObject:@"123"]; [mSet addObject:@"321"]; [mSet addObject:@"567"]; NSLog(@"%@", mSet);
删除元素
[mSet removeObject:@"567"]; NSLog(@"%@", mSet);
删除所有元素
[mSet removeAllObjects]; NSLog(@"%@", mSet);
数组
1.有序的
2.元素是对象
3.元素不唯一
4.通过下标访问数组的某个元素
5.(元素1, 元素2, 元素3, ...)
字典
1.无序的
2.键值对
3.key值唯一, value值不唯一
4.通过key值访问对应的value
5.{key1 = value1; key2 = value2; key3 = value3; ...}分号隔开
集合
1.无序的
2.元素必须是对象
3.元素唯一
4.随机访问元素
5.{(元素1, 元素2, 元素3, ...)}
基本数据类型 <-> 对象
1.NSString
2.NSNumber