Objective-C语法之KVC使用 有图有真相
Objective-C語法之KVC使用
除了一般的賦值和取值的方法,我們還可以用Key-Value-Coding(KVC)鍵值編碼來訪問你要存取的類的屬性。
下圖來自蘋果官網:
?
如何使用KVC存取對象屬性呢?看個示例
1、使用KVC
定義一個Student類,繼承于NSObject。
.h文件
#import <Foundation/Foundation.h>@interface Student : NSObject {NSString *name; } @end.m文件
#import "Student.h"@implementation Student@end.m文件也沒有實現。name屬性沒有加property,原來的訪問方法就訪問不了name屬性了。怎么辦呢?用kvc就可以了
#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]?學生姓名:張三
張三 這個值存進去了,通過valueForKey取出來了。
如果存的時候key和類屬性的名稱不一致會怎么樣呢?
代碼改成?
? ? ? ??[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。
2、鍵路徑訪問屬性
如果訪問這個類里中的屬性中的屬性呢?那就用到了鍵路徑?
關鍵字:鍵路徑取值valueForKeyPath 鍵路徑存值:forKeyPath
新建一個類Course,課程類,課程類有課程名稱這個屬性
.h文件
#import <Foundation/Foundation.h>@interface Course : NSObject {NSString *CourseName; } @end.m文件
#import "Course.h"@implementation Course@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]?課程名稱:數學課
3、自動封裝基本數據類型
我們在Student類中添加分數屬性 NSInteger point;
.h文件
#import <Foundation/Foundation.h> @class Course; @interface Student : NSObject {NSString *name;Course *course;NSInteger point; } @end.m文件不改變
#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類型的,存取都沒有問題。
4、操作集合
在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。
著作權聲明:本文由http://www.cnblogs.com/stoic/原創,歡迎轉載分享。請尊重作者勞動,轉載時保留該聲明和作者博客鏈接,謝謝!轉載于:https://www.cnblogs.com/zhidao-chen/archive/2012/08/06/2624880.html
總結
以上是生活随笔為你收集整理的Objective-C语法之KVC使用 有图有真相的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DIV+CSS布局,第五课,DOCTYP
- 下一篇: Java基础——3