iOS進階_KVC
阿新 • • 發佈:2018-12-07
KVC(Key-value coding)
鍵值編碼
基本使用
例項:
WTPerson.h
#import <Foundation/Foundation.h>
@interface WTPerson : NSObject{
// @public //@protect預設
NSString * _name;
}
/** name **/
//@property(nonatomic,strong)NSString * name;
@end
ViewController.m
#import "ViewController.h" #import "WTPerson.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UITextField *text; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; WTPerson * p = [WTPerson new]; //訪問成員變數 //p.name = @"wt"; //NSLog(@"%@",p.name); //訪問私有變數(必須要要設定為public才可訪問) //p->_name = @"wt"; //NSLog(@"%@",p->_name); //KVC(即使不用public修飾,也可以訪問私有變數) [p setValue:@"wt" forKey:@"name"]; NSLog(@"%@",[p valueForKey:@"name"]); [self.text setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"]; }
KVC賦值取值過程分析和自定義及異常處理(沒寫完,週一寫)
在專案中
commond+shift+o
搜尋setValue:forKey
發現在Foundation
框架下的NSKeyValueCoding
檔案下
我們檢視這個檔案中的方法,發現這個檔案中是一些分類的集合