Key-Value-Coding(KVC)

Objective-C語法之KVC的使用

 

除了通常的賦值和取值的方法,咱們還能夠用Key-Value-Coding(KVC)鍵值編碼來訪問你要存取的類的屬性。html

下圖來自蘋果官網:數組

 

如何使用KVC存取對象屬性呢?看個示例app

一、使用KVC

定義一個Student類,繼承於NSObject。函數

.h文件post

 

#import <Foundation/Foundation.h>

@interface Student : NSObject
{
    NSString *name;
}
@end
.m文件

 

#import "Student.h"
@implementation Student
@end

 

.m文件也沒有實現。name屬性沒有加property,原來的訪問方法就訪問不了name屬性了。怎麼辦呢?用kvc就能夠了this

 

#import "Student.h"
int main(int argc, const char * argv[])
{
    @autoreleasepool {
        Student *student = [[[Student alloc]init ]autorelease];
        [student setValue:@"張三" forKey:@"name"];
        NSString *name = [student valueForKey:@"name"];
        NSLog(@"學生姓名:%@",name);
    }
    return 0;
}

 

打印結果:編碼

2012-07-20 15:04:09.920 objectiveC[1977:403] 學生姓名:張三url

張三 這個值存進去了,經過valueForKey取出來了。spa

若是存的時候key和類屬性的名稱不一致會怎麼樣呢?code

代碼改爲 

        [student setValue:@"張三forKey:@"name1"];

運行,程序崩潰 ,打印:

2012-07-20 15:09:40.432 objectiveC[2069:403] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Student 0x106f14270> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key name1.'

提示沒有這個name1 這個key。

二、鍵路徑訪問屬性

若是訪問這個類裏中的屬性中的屬性呢?那就用到了鍵路徑 

關鍵字:鍵路徑取值valueForKeyPath 鍵路徑存值:forKeyPath

新建一個類Course,課程類,課程類有課程名稱這個屬性

.h文件

#import <Foundation/Foundation.h>

@interface Course : NSObject
{
    NSString *CourseName;
}
@end
.m文件
#import "Student.h"
@implementation Student
@end

在Student中添加Course屬性 ,student.h文件中代碼以下:

 

#import <Foundation/Foundation.h>
@class Course;
@interface Student : NSObject
{
    NSString *name;
    Course *course;
}
@end

 

實現仍是什麼都沒有,這裏就不貼代碼了

在main方法中,咱們實驗經過鍵路徑訪問Course中CourseName的屬性

 

#import "Student.h"
#import "Course.h"
int main(int argc, const char * argv[])
{
    @autoreleasepool {
        Student *student = [[[Student alloc]init ]autorelease];
        [student setValue:@"張三" forKey:@"name"];
        NSString *name = [student valueForKey:@"name"];
        NSLog(@"學生姓名:%@",name);
        
        Course *course = [[[Course alloc]init] autorelease];
        [course setValue:@"語文課" forKey:@"CourseName"];
        [student setValue:course forKey:@"course"];
        NSString *courseName = [student valueForKeyPath:@"course.CourseName"];
        NSLog(@"課程名稱:%@", courseName);
        
        //也能夠這樣存值
        [student setValue:@"數學課" forKeyPath:@"course.CourseName"];
        courseName = [student valueForKeyPath:@"course.CourseName"];
        NSLog(@"課程名稱:%@", courseName);
        
    }
    return 0;
}

 

運行打印結果:

2012-07-20 15:33:51.902 objectiveC[2415:403] 學生姓名:張三

2012-07-20 15:33:51.904 objectiveC[2415:403] 課程名稱:語文課

2012-07-20 15:33:51.904 objectiveC[2415:403] 課程名稱:數學課

三、自動封裝基本數據類型

咱們在Student類中添加分數屬性 NSInteger point;

.h文件

#import <Foundation/Foundation.h>
@class Course;
@interface Student : NSObject
{
    NSString *name;
    Course *course;
    NSInteger point;
}
@end
.m文件不改變就無論了

下面是main示例了

 

#import "Student.h"
#import "Course.h"

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        Student *student = [[[Student alloc]init ]autorelease];
        [student setValue:@"張三" forKey:@"name"];
        NSString *name = [student valueForKey:@"name"];
        NSLog(@"學生姓名:%@",name);
        
        Course *course = [[[Course alloc]init] autorelease];
        [course setValue:@"語文課" forKey:@"CourseName"];
        [student setValue:course forKey:@"course"];
        NSString *courseName = [student valueForKeyPath:@"course.CourseName"];
        NSLog(@"課程名稱:%@", courseName);
        
        //也能夠這樣存值
        [student setValue:@"數學課" forKeyPath:@"course.CourseName"];
        courseName = [student valueForKeyPath:@"course.CourseName"];
        NSLog(@"課程名稱:%@", courseName);
        
        [student setValue:@"88" forKeyPath:@"point"];
        NSString *point = [student valueForKey:@"point"];
        NSLog(@"分數:%@", point);
        
    }
    return 0;
}

 

打印結果:

2012-07-20 15:43:19.593 objectiveC[2533:403] 學生姓名:張三

2012-07-20 15:43:19.596 objectiveC[2533:403] 課程名稱:語文課

2012-07-20 15:43:19.596 objectiveC[2533:403] 課程名稱:數學課

2012-07-20 15:43:19.598 objectiveC[2533:403] 分數:88

咱們用NSString*類型設置的屬性值@"88",而咱們的屬性是NSInteger類型的,存取都沒有問題。

四、操做集合

在Student類中加入數組NSArray,用來表示其餘的學生。這樣咱們能夠添加多個其餘的學生,再用集合操做計算學生的分數,最高分,最低分,平均分等

#import <Foundation/Foundation.h>
@class Course;
@interface Student : NSObject
{
    NSString *name;
    Course *course;
    NSInteger point;
    NSArray *otherStudent;
}
@end

.m文件實現不變。

在main函數中添加三個學生,添加到數組中,而後求平均分,最高,最低分,學生數量

#import "Student.h"
#import "Course.h"
int main(int argc, const char * argv[])
{
    @autoreleasepool {
        Student *student = [[[Student alloc]init ]autorelease];
        [student setValue:@"張三" forKey:@"name"];
        NSString *name = [student valueForKey:@"name"];
        NSLog(@"學生姓名:%@",name);
        
        [student setValue:@"88" forKey:@"point"];
        NSString *point = [student valueForKey:@"point"];
        NSLog(@"分數:%@", point);
        
        Student *student1 = [[[Student alloc]init]autorelease];
        Student *student2 = [[[Student alloc]init]autorelease];
        Student *student3 = [[[Student alloc]init]autorelease];
        [student1 setValue:@"65" forKey:@"point"];
        [student2 setValue:@"77" forKey:@"point"];
        [student3 setValue:@"99" forKey:@"point"];
        NSArray *array = [NSArray arrayWithObjects:student1,student2,student3,nil];
        [student setValue:array forKey:@"otherStudent"];
        NSLog(@"其餘學生的成績%@", [student valueForKeyPath:@"otherStudent.point"]);
        NSLog(@"共%@個學生", [student valueForKeyPath:@"otherStudent.@count"]);
        NSLog(@"最高成績:%@", [student valueForKeyPath:@"otherStudent.@max.point"]);
        NSLog(@"最低成績:%@", [student valueForKeyPath:@"otherStudent.@min.point"]);
        NSLog(@"平均成績:%@", [student valueForKeyPath:@"otherStudent.@avg.point"]);
    }
    return 0;
}

運行打印結果

2012-07-20 16:09:17.101 objectiveC[2857:403] 學生姓名:張三

2012-07-20 16:09:17.104 objectiveC[2857:403] 分數:88

2012-07-20 16:09:17.105 objectiveC[2857:403] 其餘學生的成績(

    65,

    77,

    99

)

2012-07-20 16:09:17.106 objectiveC[2857:403] 共3個學生

2012-07-20 16:09:17.106 objectiveC[2857:403] 最高成績:99

2012-07-20 16:09:17.107 objectiveC[2857:403] 最低成績:65

2012-07-20 16:09:17.108 objectiveC[2857:403] 平均成績:80.333333333333333333333333333333333333

還能夠求總和  @sum。

本站公眾號
   歡迎關注本站公眾號,獲取更多信息