1.NSDictionary
1.1由鍵值對組成,關鍵字不允許重複,值可以重複
1.2創建方法
NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:@"one",@"1",@"two",@"2",@"three",@"3", nil];//標準方法
NSDictionary *dict2 = @{@"1":@"one",@"2":@"two",@"3":@"three"};//優化後的方法,最常用
NSArray *object = @[@"one",@"two",@"three"];//存值
NSArray *key = @[@"1",@"2",@"3"];//存關鍵字
NSDictionary *dict3 = [NSDictionary dictionaryWithObjects:object forKeys:key];//將數組轉換成字典
NSDictionary *dict4 = [NSDictionary dictionaryWithDictionary:dict3];//創建dict3的副本
1.3求鍵值對個數
NSLog(@"%lu",dict3.count);
1.4將字典中的所有值轉換成數組
NSArray *object2 = [dict3 allValues];
1.5將字典中的所有關鍵字轉換成數組
NSArray *key2 = [dict3 allKeys];
1.6通過關鍵字找到對應值
//獲取字典中所有值,即將字典轉換成數組的方法
NSArray *object2 = [dict3 allValues];
NSArray *key2 = [dict3 allKeys];
NSLog(@"%@",object2);
NSLog(@"%@",key2);
//根據一個關鍵字查找一個值
NSString *str = [dict3 objectForKey:@"1"];//標準方法
NSLog(@"%@",str);
str = dict3[@"1"];
NSLog(@"%@",str);//優化後的方法,最常用的方法
//根據多個關鍵字來找多個值
NSArray *key1 = @[@"1",@"4"];
NSArray *obj1 = [dict3 objectsForKeys:key1 notFoundMarker:@"沒有對應的值"];
NSLog(@"%@",obj1);
for(NSString *str1 in obj1)
NSLog(@"%@",str1);
1.7通過值找對應的關鍵字
NSArray *key3 = [dict3 allKeysForObject:@"three"];
1.8對字典中的值排序
-(NSComparisonResult)compareAge:(SHStudent *)other
{
NSNumber *selfAge = [NSNumber numberWithInt:self.age];
NSNumber *otherAge = [NSNumber numberWithInt:other.age];
return [otherAge compare:selfAge];
}
SHStudent *stu1 = [SHStudent studentWithName:@"zhangsan" andAge:18];
SHStudent *stu2 = [SHStudent studentWithName:@"lisi" andAge:20];
SHStudent *stu3 = [SHStudent studentWithName:@"wangwu" andAge:19];
NSDictionary *stu = @{@"1":stu1,@"2":stu2,@"3":stu3};
NSArray *sortedAge = [stu keysSortedByValueUsingSelector:@selector(compareAge:)];
for(NSString *key in sortedAge)
{
NSLog(@"%@ = %@",key,stu[key]);
}
1.9遍歷
NSEnumerator * e = [dict3 objectEnumerator];//枚舉器值遍歷
while (str = [e nextObject]) {
NSLog(@"%@",str);
}
e = [dict3 keyEnumerator];//枚舉器關鍵字遍歷
while (str = [e nextObject]) {
NSLog(@"%@",str);
}
for(NSString *str in dict3)//快速遍歷
NSLog(@"%@ %@",str,dict3[str]);
2.0字典的文件讀寫
[dict3 writeToFile:@"/Users/tarena/Desktop/dict3.plist" atomically:YES];
NSDictionary *dict5 = [NSDictionary dictionaryWithContentsOfFile:@"/Users/tarena/Desktop/dict3.plist"];
NSLog(@"%@",dict5);
2.NSMutableDictionary
2.1可變字典,是NSDictionary的子類
2.2創建方法
NSMutableDictionary *dict1 = [NSMutableDictionary dictionary];
NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithCapacity:100];
NSMutableDictionary *dict3 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"one",@"1",@"two",@"2",@"three",@"3", nil];//標準方法
NSMutableDictionary *dict4 = @{@"1":@"one",@"2":@"two",@"3":@"three"};//退化成不可變字典
2.3添加方法
[dict3 setValue:@"four" forKey:@"4"];
NSLog(@"%@",dict3);
NSDictionary *added = @{@"5":@"five",@"6":@"six",@"7":@"seven"};
[dict3 addEntriesFromDictionary:added];//添加多個鍵值對
NSLog(@"%@",dict3);
2.4覆蓋方法
NSDictionary *dict5 = @{@"1":@"one",@"2":@"two",@"3":@"three"};
NSMutableDictionary *dict6 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"aaaaa",@"1",@"bbbb",@"2",@"cccc",@"3", nil];
[dict6 setDictionary:dict5];
NSLog(@"%@",dict6);
2.5刪除方法
[dict3 removeObjectForKey:@"1"];//刪除指定關鍵字對應的鍵值對
NSLog(@"%@",dict3);
NSArray *del = @[@"3",@"5"];
[dict3 removeObjectsForKeys:del];
NSLog(@"%@",dict3);
[dict3 removeAllObjects];
NSLog(@"%lu",dict3.count);
3.Block
3.1是一種新的數據類型
3.2Block類型的變量中存儲的是一段兒程序代碼
3.3語法
#import <Foundation/Foundation.h>
void fun()
{
NSLog(@"函數被執行了");
}
void(^myBlock)(void) = ^void(void)
{
NSLog(@"Block被執行了");
};
double(^myBlock1)(int,int) = ^double(int a,int b)
{
return a+b;
};
typedef void(^BlockType)(void);
int main(int argc, const char * argv[]) {
@autoreleasepool {
// fun();
myBlock();
void (*pf)() = fun;
pf();
NSLog(@"%lg",myBlock1(5,6));
BlockType b1;
b1 = ^void(void)
{
NSLog(@"用block類型定義的變量");
};
b1();
}
return 0;
}
3.4Block與全局變量
3.5Block與局部變量
3.6Block在自定義類中的使用
#import <Foundation/Foundation.h>
#import "SHMyClass.h"
int g_i=0;
void fun()
{
NSLog(@"函數被執行了");
}
void(^myBlock)(void) = ^void(void)
{
NSLog(@"Block被執行了");
};
double(^myBlock1)(int,int) = ^double(int a,int b)
{
return a+b;
};
typedef void(^BlockType)(void);
typedef double(^MyBlock1)(int,int);
void fa(MyBlock1 a)
{
NSLog(@"%lg",a(3,5));
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
// fun();
myBlock();
void (*pf)() = fun;
pf();
NSLog(@"%lg",myBlock1(5,6));
BlockType b1;
b1 = ^void(void)
{
NSLog(@"用block類型定義的變量");
};
b1();
MyBlock1 b2 = ^double(int a,int b)
{
return a + b;
};
NSLog(@"%lg",b2(2,3));
fa(^double(int a,int b){return a + b;});
fa(^double(int a,int b){return a * b;});
MyBlock1 b3 = ^double(int a,int b)
{
a *= g_i;//全局變量可以被訪問
g_i = 20;//全局變量可以被賦值
return a + b;
};
NSLog(@"b3 = %lg",b3(2,3));
NSLog(@"全局變量:%d",g_i);
int i2= 30;
__block int i3 = 50;
BlockType b4 = ^void(void)
{
NSLog(@"%d",i2);//局部變量可以在block中被訪問
// i2 = 40;//局部變量不能在block中被賦值
i3 = 60;//加上關鍵字__block後,局部變量可以在Block中被賦值
};
b4();
NSLog(@"i3 = %d",i3);
//Block在自定義類中的使用
SHMyClass *myc = [[SHMyClass alloc]init];
[myc method];
[myc method:^void(void){NSLog(@"沒得說了很強");}];
[myc method:^void(void){NSLog(@"隨便說說");}];
[myc method2:^double(int a,int b){return a+b;}];
[myc method3:^double(int x,int y)
{
return x * y;
}andX:3 andY:2];
[myc getBlock]();
myc.b = ^void(void){NSLog(@"block做屬性");};
myc.b();
}
return 0;
}
思考練習
定義一個學生類
1.屬性:姓名、年齡
2.方法:初始化、工廠、show
在主函數中,定義5個學生類的對象,分別用數組、字典存放,並用Block排序
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
解析
SHStudent類:
#import <Foundation/Foundation.h>
@interface SHStudent : NSObject
@property NSString *name;
@property int age;
-(id)initWithName:(NSString*)name andAge:(int)age;
+(id)studentWithName:(NSString*)name andAge:(int)age;
-(void)show;
@end
#import "SHStudent.h"
@implementation SHStudent
-(id)initWithName:(NSString *)name andAge:(int)age
{
if (self = [super init])
{
self.name = name;
self.age = age;
}
return self;
}
+(id)studentWithName:(NSString *)name andAge:(int)age
{
__autoreleasing SHStudent *s = [[SHStudent alloc] initWithName:name andAge:age];
return s;
}
-(void)show
{
NSLog(@"姓名:%@,年齡:%d", self.name, self.age);
}
-(NSString *)description
{
return [NSString stringWithFormat:@"姓名:%@,年齡:%d", self.name, self.age];
}
@end
main函數:
#import <Foundation/Foundation.h>
#import "SHStudent.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
SHStudent *stu1 = [[SHStudent alloc] initWithName:@"張三" andAge:18];
SHStudent *stu2 = [[SHStudent alloc] initWithName:@"李四" andAge:20];
SHStudent *stu3 = [[SHStudent alloc] initWithName:@"王五" andAge:19];
SHStudent *stu4 = [[SHStudent alloc] initWithName:@"趙六" andAge:22];
SHStudent *stu5 = [[SHStudent alloc] initWithName:@"錢七" andAge:21];
NSArray *stu = @[stu1, stu2, stu3, stu4, stu5];
NSArray *sorted = [stu sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
NSNumber *obj1Age = [NSNumber numberWithInt:[obj1 age]];
NSNumber *obj2Age = [NSNumber numberWithInt:[obj2 age]];
return [obj1Age compare:obj2Age];
}];
for (SHStudent *s in sorted)
{
NSLog(@"%@", s);
}
NSDictionary *dict = @{@"1":stu1,@"2":stu2,@"3":stu3,@"4":stu4,@"5":stu5};
sorted = [dict keysSortedByValueUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
NSNumber *obj1Age = [NSNumber numberWithInt:[obj1 age]];
NSNumber *obj2Age = [NSNumber numberWithInt:[obj2 age]];
return [obj1Age compare:obj2Age];
}];
for (NSString *key in sorted)
{
NSLog(@"%@=%@", key, dict[key]);
}
}
return 0;
}