第一次寫簡書
在簡書上學到很多iOS知識,今天第一次分享文章利赋,希望以后自己有時間多總結云芦,發(fā)布文章整理贪惹,利人利己察蹲。如有不對的地方济锄,請大家指正聊浅!
KVC
KVC(key-value-coding),鍵值編碼餐抢。說白了就是通過類屬性的key獲取其屬性的value现使,而不用通過Setter,Getter,這個在開發(fā)中很實用旷痕。
舉個例子:
Student 類有2個屬性
// 名字
@property (nonatomic, copy)NSString *name;
// 分數(shù)
@property (nonatomic, assign)float score;
設置Student值碳锈,2種方法可達同樣效果
Student *student1 = [[Student alloc] init];
student1.name = @"zhangsan";
student1.score = 100;
Student *student2 = [[Student alloc] init];
[student2 setValue:@"lisi" forKeyPath:@"name"];
[student2 setValue:@99 forKeyPath:@"score"];
開發(fā)中常用的案例:
1,替換UITabBarController的tabBar
// 更換tabBar
[self setValue:[[MyTabBar alloc] init] forKeyPath:@"tabBar"];
B3657847A5C6DD1DE909A375F7597F73.jpg
2欺抗,設置UITextField占位符的顏色售碳,F(xiàn)ont
[textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
[textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
KVC,用處很大,功能很強大绞呈,有興趣的可以繼續(xù)深入研究贸人。
KVO
KVO(key-value-observing),建立在KVC,利用類屬性key監(jiān)聽其值的變化。說白了佃声,就是屬性值一旦有變化就通知觀察者艺智。
使用步驟:
1,注冊監(jiān)聽
2圾亏,設置監(jiān)聽事件(如zhangsan的score成績一旦變化就告訴他媽)
3十拣,注銷監(jiān)聽
以上面的student1對象實例為例:
// 監(jiān)聽
[student1 addObserver:self forKeyPath:@"score" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
// 監(jiān)聽這個方法回調
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
// 在這個方法里面監(jiān)聽屬性變化,如zhangsan的score成績一旦變化就告訴他媽咪 O(∩_∩)O~
}
- (void)dealloc{
// 注銷
[self.student1 removeObserver:self forKeyPath:@"score"];
}
常用開發(fā)案例
1志鹃,監(jiān)聽UITableVIew的contentOffset,根據(jù)表格偏移量輕松改變navigationBar的位置夭问,顏色等設置
[tableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
2,一般APP都有編輯個人信息頁面曹铃,利用KVO監(jiān)聽用戶編輯的信息即刻更新UI并且提交新數(shù)據(jù)到服務器甲喝,無需再為每個修改項寫處理函數(shù)。
8B01CF602F8C0B5F5CF4E3A67A20CCC8.jpg
補充KVC可以放R大招
你還在用for循環(huán)計算集合數(shù)據(jù)的總數(shù)铛只,平均值埠胖?那你就out了,Q,W,E,R直接搞掂淳玩。
Student *student1 = [[Student alloc] init];
student1.name = @"zhangsan";
student1.score = 100;
Student *student2 = [[Student alloc] init];
[student2 setValue:@"lisi" forKeyPath:@"_name"];
[student2 setValue:@99 forKeyPath:@"score"];
Student *student3 = [[Student alloc] init];
[student3 setValue:@"wangwu" forKeyPath:@"_name"];
[student3 setValue:@71 forKeyPath:@"score"];
Teacher *teacher = [[Teacher alloc] init];
teacher.name = @"Miss Lu";
teacher.studentArray = [NSMutableArray array];
[teacher.studentArray addObject:student1];
[teacher.studentArray addObject:student2];
[teacher.studentArray addObject:student3];
// 平均分數(shù)
NSNumber *avgSccore = [teacher valueForKeyPath:@"studentArray.@avg.score"];
NSLog(@"%@", avgSccore);
// 總分
NSNumber *totalScore = [teacher valueForKeyPath:@"studentArray.@sum.score"];
NSLog(@"%@", totalScore);
// 最低分直撤,最高分
NSNumber *min = [teacher valueForKeyPath:@"studentArray.@min.score"];
NSLog(@"%@", min);
NSNumber *max = [teacher valueForKeyPath:@"studentArray.@max.score"];
NSLog(@"%@", max);
// 求同學數(shù)量
NSArray *studentCount = [teacher valueForKeyPath:@"studentArray.@count"];
NSLog(@"%@", studentCount);
// 獲取所有同學的分數(shù)
NSArray *sccoreArray = [teacher valueForKeyPath:@"studentArray.score"];
NSLog(@"%@", sccoreArray);
如有不對的地方,請大家指正蜕着!