KVC模式講解和Block語法

KVC鍵值編碼,使用完整實例:

#import <Foundation/Foundation.h>

@interface Course : NSObject

{

    NSString* courseName;

}

- (NSString*)description;

@end

 

#import "Course.h"

@implementation Course

- (NSString*)description

{

    NSString* str = [NSString stringWithFormat:@"%@",courseName];

    return str;

}

@end

 

#import <Foundation/Foundation.h>

#import "Course.h"

@interface Student : NSObject

{

    NSString* name;

    Course* course;

    NSInteger point;

    NSArray* otherStudent;

}

- (NSString*)description;

@end

 

#import "Student.h"

@implementation Student

- (NSString*)description

{

    NSString* str = [NSString stringWithFormat:@"name is %@,course is %@,point is %lu",name,course,point];

    return str;

}

@end

 

#import <Foundation/Foundation.h>

#import "Student.h"

#import "Course.h"

 

int main(int argc, const char * argv[])

{

 

@autoreleasepool {

        //使用KVC存儲屬性值,

        //方式一

        Student* student1 = [[[Student alloc] init] autorelease];

        Course* course1 = [[[Course alloc] init] autorelease];

        [student1 setValue:@"wusong" forKey:@"name"];

        [student1 setValue:@"90" forKey:@"point"];

        [course1 setValue:@"yuwen" forKey:@"courseName"];

        [student1 setValue:course1 forKey:@"course"];

       

        //方式二

        Student* student2 = [[[Student alloc] init] autorelease];

        Course* course2 = [[[Course alloc] init] autorelease];

        [student2 setValue:@"liyang" forKey:@"name"];

        [student2 setValue:course2 forKey:@"course"];

        [student2 setValue:@"shuxue" forKeyPath:@"course.courseName"];

        //自動封裝基本數據類型

        [student2 setValue:@"88" forKey:@"point"];

       

        //定義三個學生,存放到數組中,求三個學生成績的最大值,最小值,平均值

        Student* student = [[[Student alloc] init] autorelease];

        Course* course = [[[Course alloc] init] autorelease];

        [student setValue:@"wusong" forKey:@"name"];

        [student setValue:course forKey:@"course"];

        [student setValue:@"shuxue" forKeyPath:@"course.courseName"];

       

        Student* stu1 = [[[Student alloc] init] autorelease];

        Student* stu2 = [[[Student alloc] init] autorelease];

        Student* stu3 = [[[Student alloc] init] autorelease];

        [stu1 setValue:@"88" forKey:@"point"];

        [stu2 setValue:@"70" forKey:@"point"];

        [stu3 setValue:@"91" forKey:@"point"];

        NSArray* array = [NSArray arrayWithObjects:stu1,stu2,stu3, nil];

        [student setValue:array forKey:@"otherStudent"];

     

        NSLog(@"其他學生的成績%@",[student valueForKeyPath:@"otherStudent.point"]);

        NSLog(@"共有%@個學生",[student valueForKeyPath:@"otherStudent.@count"]);

        NSLog(@"最高成績爲%@",[student valueForKeyPath:@"[email protected]"]);

        NSLog(@"最低成績爲%@",[student valueForKeyPath:@"[email protected]"]);

        NSLog(@"平均成績爲%@",[student valueForKeyPath:@"[email protected]"]);

 }

 return 0;

}

 

Block語句塊

        //當在BLock語句塊外的變量用__block聲明時,在Block語句塊中此變量纔可以被修改

        __block int num = 10;

       

        void(^myBlock)(int n1,int n2) = ^(int num1,int num2){

            NSLog(@"%d,%d",num1,num2);

            num++;

            NSLog(@"%d",num);

        };

       

        myBlock(1,2);//調用

 

        //使用Block語法來對數組中的元素進行排序和枚舉訪問數組中的元素,步驟爲:

        NSArray* array = [NSArray arrayWithObjects:@"string1",@"string12",@"string21",@"string01",nil];

        NSComparator comparator = ^(id obj1,id obj2){

            return [obj1 compare:obj2];

        };

       

        NSArray* array1 = [array sortedArrayUsingComparator:comparator];

        NSLog(@"array1:%@",array1);

       

        //枚舉,逐一去訪問元素

        [array1 enumerateObjectsUsingBlock:^(id obj,NSUInteger index,BOOL* stop){

            NSLog(@"%@",obj);

            NSLog(@"%lu",index);

        }];

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章