您好,登錄后才能下訂單哦!
切記:請求的數(shù)據(jù)要存在相應(yīng)的類中,不能在加載試圖中請求數(shù)據(jù)
KVC就是對請求數(shù)據(jù)的一個簡化
MainViewController.m
#import "MainViewController.h" #import "Student.h" @interface MainViewController () @end @implementation MainViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor cyanColor]; //KVC的使用 Student *stu = [[Student alloc] init]; //KVC的作用:對一個對象的成員變量進行操作(賦值/取值) //賦值的方法 [stu setValue:@"adfasdf" forKey:@"name"]; // [stu setValue:@"米4像蘋果1" forKey:@"Name"]; //第一個查找 // [stu setValue:@"米4像蘋果2" forKey:@"_Name"]; //找不到 // [stu setValue:@"米4像蘋果3" forKey:@"_name"]; // //這個是一個類中的屬性賦值 // stu setValue:<#(id)#> forKeyPath:<#(NSString *)#> NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"222", @"StudentID", @"liuyafang", @"name" ,nil]; [stu setValuesForKeysWithDictionary:dic]; //取值的方法 NSLog(@"%@",[stu valueForKey:@"name"]); //KVO 鍵值觀察 (注冊一個觀察者)(自己監(jiān)視自己對象的內(nèi)容) //參數(shù)1:觀察誰 //參數(shù)2:觀察哪個屬性 //參數(shù)3:在實現(xiàn)方法中獲得新值合適舊值 //參數(shù)4:任意的指針類型 [stu addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:@"aaa"]; stu.name = @"pingguo45"; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { NSLog(@"%@",keyPath); NSLog(@"%@",object); NSLog(@"%@",change); NSLog(@"%@",context); } - (void)dealloc { // self removeObserver:<#(NSObject *)#> forKeyPath:<#(NSString *)#> [super dealloc]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
Student.h
#import <Foundation/Foundation.h> @interface Student : NSObject @property (nonatomic , retain)NSString *name; @property (nonatomic , retain)NSString *sex; @property (nonatomic , retain)NSString *studengID; //利用kvc對model進行封裝 - (instancetype) initWihtDictionary:(NSDictionary *)dic; @end
Student.m
#import "Student.h" @implementation Student - (void)setValue:(id)value forUndefinedKey:(NSString *)key { //這個方法在類的內(nèi)部實現(xiàn),就用就是糾錯 //一旦在賦值過程中,發(fā)現(xiàn)key沒有周到對應(yīng)的成員變量,就會調(diào)用這個發(fā)放 //如果沒有重寫個方法,就會crash if ([key isEqualToString:@"id"]) { _studengID = value; } } - (id)valueForUndefinedKey:(NSString *)key { //取值的糾錯方法 if ([key isEqualToString:@"id"]) { return self.superclass; } return nil; } - (instancetype) initWihtDictionary:(NSDictionary *)dic { self = [super init]; if (self) { [self setValuesForKeysWithDictionary:dic]; } return self; } - (void)dealloc { [_name release]; [_sex release]; [super dealloc]; } @end
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。