本文主要是介绍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);
}];
这篇关于KVC模式讲解和Block语法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!