NSDictionary
1.初始化
NSDictionary *dict1 = [[NSDictionary alloc]initWithObjectsAndKeys:@“a”,@“1”,@“b”,@“2”,@“c”,@“3”,nil];
2.初始化方法2
NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:@“a”,@“1”,@“b”,@“2”,@“c”,@“3”,nil];
3.通过字典赋值字典
NSDictionary *dict3 = [NSDictionary dictionaryWithDictionary:dict1];
4.通过数组赋值字典
NSArray *valueArr = [NSArray arrayWithObjects:@"a",@"b",@"c", nil];
NSArray *keyArr = [NSArray arrayWithObjects:@"1",@"2",@"3", nil];
NSDictionary *dict4 = [NSDictionary dictionaryWithObjects:valueArr forKeys:keyArr];
5.字面量
NSDictionary *dict5 = @{@“a”:@“1”,@“b”:@“2”,@“c”:@“3"};
6.通过key获得value
NSDictionary *dict = @{@“1”:@“周杰伦”,@“2”:@“庾澄庆”,@“3”:@“那英”,@“4”:@“汪峰”};
NSLog(@“%@“,[dict objectForKey:@“1”]);
7.获得所有key
NSLog(@“%@”,[dict allkey]):
8.获得所有value
NSLog(@“%@“,[dict allvalue]);
9.获得键值对个数
NSLog(@“%lu”,[dict count]);
NSMutableDictionary(可变字典)
1.赋值
NSMutableDictionary *mDict1 = [NSMutableDictionary dictionaryWithObjectsAndKey:@“a”,@“1”,@“b”,@“2”,@“c”,@“3”,nil];
2.添加
[mDict1 setObject@“d” forKey:@“4”];
3.修改
[mDict1 setObject:@“e” forKey:@“1"];
4.删除
[mDict1 removeObjectForKey:@“1”];
[mDict1 removeAllObjects];
NSArray *keyarr = [NSArray arrayWithObjects:@“4”,@“3”,nil];
[mDict1 removeObjectsForKeys:keyarr]
5.查找
NSLog(@“%@“,[mDict1 objectForKey:@“2”]);
6.遍历
1⃣️一个一个
for (int i = 0; i < [[mDict1 allValues]count]; i++) {
NSArray *temp = [mDict1 allValues];
NSLog(@"%@",temp);
}
NSArray *temp = [mDict1 allValues];
NSLog(@"%@",temp);
}
NSArray * tempArr = [mDict1 allKeys];
2⃣️通过所有的key打印所有的value
for (int i = 0; i < [tempArr count]; i++) {
NSLog(@"%@",[mDict1 objectForKey:tempArr[i]]);}
NSSet(集合)
1.初始化
NSSet *set1 = [[NSSet alloc]initWithObjects:@“a”,@“b”,@“c”,nil];
2.初始化2
NSSet *set2 = [NSSet setWithObjects:@“a”,@“b”,@“c”,nil];
3.通过集合赋值集合
NSSet *set3 = [NSSet setWithSet:set2];
4.通过数组赋值集合
NSArray *setArr = [NSArray arrayWithObjects:@“a”,@“b”,@“c”,nil
];
NSSet *set = [NSSet setWithArray:setArr];
5.个数
NSLog(@“%lu”,[set2 count]);
6.判断
NSLog(@“%d”,[set1 containsObject:@“b”]);
7.查找
NSLog(@“%@“,[set1 anyObject]);
NSLog(@“%@“,[set1 allObjects]);
NSMutableSet(可变集合)
1.初始化
NSMutableSet *ms1 = [NSMutableSet setWithObjects:@“a”,@“b”,@“c”,nil];
2.增加对象
[ms1 addObject:@“d”];
3.删除对象
[ms1 removeObject:@“d"];
[ms1 removeAllObjects];
4.修改方法
[ms1 setSet:set1];
[ms1 addObject:@“a”];
NSCountedSet(NSMutableSet的子类)
1.赋值
NSCountedSet *ctSet1 = [NSCountedSet setWithObjects:@“a”,@“b”,@“c”,nil];
2.增加
[ctSet1 addObject:@“d”];
[ctSet1 addObject:@“b”];
3.个数(NSUIntager)
NSLog(@“%lu”,[ctSet1 count]);
4.统计重复元素的个数
NSLog(@“%lu”,[ctSet1 countForObject:@“a"]);
快速枚举遍历
1快速遍历一个数组
NSArray *ar = [NSArray arrayWithObjects:@“a”,@“b”,@“c”,nil];
for(NSString * s in ar)
{ NSLog(@“%@“,s);}
2快速遍历一个字典(遍历出来的都是key)
NSDictionary *dict11 = [NSdictionary dictionaryWithObjectsAndKeys:@“a”,@“1”,@“b”,@“2”,@“c”,@“3”,nil];
for(NSString *ss in dict11)
{ NSLog(@“%@“,ss);}
快速枚举的特点
1数组枚举得到数组中的元素对象
2字典枚举得到的是字典中的key值
3集合枚举得到的是集合中的元素对象
例子:
NSMutableArray *mArr1 = [NSMutableArray arrayWithObjects:@"daa",@"bbb",@"ccc", nil];
for (int i = 0; i < [mArr1 count] - 1; i++) {
for (int j = 0; j < [mArr1 count] - 1 - i; j++) {
if ([[mArr1 objectAtIndex:j]compare:[mArr1 objectAtIndex:j + 1]] == -1) {
[mArr1 exchangeObjectAtIndex:j withObjectAtIndex:j+1];
}
for (int i = 0; i < [mArr1 count] - 1; i++) {
for (int j = 0; j < [mArr1 count] - 1 - i; j++) {
if ([[mArr1 objectAtIndex:j]compare:[mArr1 objectAtIndex:j + 1]] == -1) {
[mArr1 exchangeObjectAtIndex:j withObjectAtIndex:j+1];
}
}
数组排序
#import "Person.h"
@implementation Person
- (NSString *)name{
return _name;
}
- (void)setName:(NSString *)name{
_name = name;
}
- (NSInteger)age{
return _age;
}
- (void)setAge:(NSInteger)age{
_age = age;
}
- (instancetype)initWithName:(NSString *)name age:(NSInteger)age{
if (self = [super init]) {
_name = name;
_age = age;
}
return self;
}
- (NSComparisonResult)compareByName:(Person *)aPerson{
if ([self.name compare:aPerson.name] == 1) {
return NSOrderedDescending;
}else if ([self.name compare:aPerson.name] == 0){
return NSOrderedSame;
}
else
return NSOrderedAscending;
}
// 格式化字符串 有一个description方法 它可以用于打印数组
- (NSString *)description{
NSString *s = [NSString stringWithFormat:@"name:%@,age:%ld",self.name,self.age];
return s;
}
- (NSComparisonResult)compareByAge:(Person *)aPerson{
if (self.age > aPerson.age) {
return NSOrderedDescending;
}else if (self.age == aPerson.age){
return NSOrderedSame;
}
else
return NSOrderedAscending;
}
@implementation Person
- (NSString *)name{
return _name;
}
- (void)setName:(NSString *)name{
_name = name;
}
- (NSInteger)age{
return _age;
}
- (void)setAge:(NSInteger)age{
_age = age;
}
- (instancetype)initWithName:(NSString *)name age:(NSInteger)age{
if (self = [super init]) {
_name = name;
_age = age;
}
return self;
}
- (NSComparisonResult)compareByName:(Person *)aPerson{
if ([self.name compare:aPerson.name] == 1) {
return NSOrderedDescending;
}else if ([self.name compare:aPerson.name] == 0){
return NSOrderedSame;
}
else
return NSOrderedAscending;
}
// 格式化字符串 有一个description方法 它可以用于打印数组
- (NSString *)description{
NSString *s = [NSString stringWithFormat:@"name:%@,age:%ld",self.name,self.age];
return s;
}
- (NSComparisonResult)compareByAge:(Person *)aPerson{
if (self.age > aPerson.age) {
return NSOrderedDescending;
}else if (self.age == aPerson.age){
return NSOrderedSame;
}
else
return NSOrderedAscending;
}
@end
#import "Person.h"
int main(int argc, const char * argv[]) {
// 有三个人 分别叫 "kobe" 18 "james" 15 "durant" 20按照姓名和年龄进行排序
Person * p1 = [[Person alloc]initWithName:@"kobe" age:18];
Person * p2 = [[Person alloc]initWithName:@"james" age:15];
Person * p3 = [[Person alloc]initWithName:@"durant" age:20];
NSMutableArray *ms1 = [NSMutableArray arrayWithObjects:p1,p2,p3, nil];
// @selector是一个关键字 指的是一个方法选择器 里面可以写方法名去实现方法
[ms1 sortUsingSelector:@selector(compareByAge:)];
NSLog(@"%@",ms1);
[ms1 sortUsingSelector:@selector(compareByName:)];
int main(int argc, const char * argv[]) {
// 有三个人 分别叫 "kobe" 18 "james" 15 "durant" 20按照姓名和年龄进行排序
Person * p1 = [[Person alloc]initWithName:@"kobe" age:18];
Person * p2 = [[Person alloc]initWithName:@"james" age:15];
Person * p3 = [[Person alloc]initWithName:@"durant" age:20];
NSMutableArray *ms1 = [NSMutableArray arrayWithObjects:p1,p2,p3, nil];
// @selector是一个关键字 指的是一个方法选择器 里面可以写方法名去实现方法
[ms1 sortUsingSelector:@selector(compareByAge:)];
NSLog(@"%@",ms1);
[ms1 sortUsingSelector:@selector(compareByName:)];
NSLog(@"%@",ms1);