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:@"otherStudent.@max.point"]);

        NSLog(@"最低成绩为%@",[student valueForKeyPath:@"otherStudent.@min.point"]);

        NSLog(@"平均成绩为%@",[student valueForKeyPath:@"otherStudent.@avg.point"]);

 }

 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);

        }];

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值