KVC设计模式讲解

本文介绍了Objective-C中的KVC(Key-Value-Coding)设计模式,用于访问和设置对象的实例变量,即使没有声明属性。通过示例展示了如何使用KVC设置和获取简单类型以及复杂类型的实例变量值,包括如何通过keyPath访问嵌套属性,并演示了如何利用KVC对数组内的对象属性进行聚合操作,如计算平均值、最大值和最小值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        在Objective-c语言中,可以用@property和@synthesize来创建实例变量的属性,因此对象访问的时候可以直接使用点语法。

        但是,如果不声明属性,如何访问到对象的实例变量呢?KVC就解决了这一问题。

        KVC  全称Key-Value-Coding,也就是键值编码。先看下面一个例子:

 @interface  Student  : NSObject

{

  NSString* name;

}

-(NSString*)description;

@end

 

@implementation Student

-(NSString*)description

{

  return [NSString stringWithFormat:@"%@",name];

}

@end

 

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

{

  @autoreleasePool{

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

    [student setValue:@"zhangsan" forKey:@"name"];//这里用到的KVC,每个从NSObject继承而来的类都继承了-(void)setValue:(id)  forKey:@"实例变量"

                                                                                            //用来设置对象的实例变量的值

    [student release];

  }

  return 0;

}

如果实例变量是另一个类的对象,看下面这个例子,Course是一个课程类,里边只有一个实例变量courseName(课程名)

Student类包含NSString* name;Course* course;NSInteger point;

则在主函数中进行设置的时候应该注意:

  Student* student = [[Student alloc] init];
        Course* course = [[Course alloc] init];
        [student setValue:@"zhangsan" forKey:@"name"];

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

       [student setValue:@"yuwen" forKeyPath:@"course.courseName"];
       [student setValue:@"88" forKey:@"point"];

下面是一个具体的实例:

#import <Foundation/Foundation.h>
#import "Course.h"

@interface Student : NSObject
{
    NSString* name;
    Course* course;
    NSInteger point;
    NSArray* otherStudent;
}
-(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 "Course.h"

@implementation Course
-(NSString*)description
{
    NSString* str = [NSString stringWithFormat:@"%@",courseName];
    return str;
}
@end

 

#import <Foundation/Foundation.h>

#import "Course.h"

#import "Student.h"

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

{

  @autoreleasePool{

        Student* student = [[Student alloc] init];
        Course* course = [[Course alloc] init];
        [student setValue:@"zhangsan" forKey:@"name"];
        [student setValue:course forKey:@"course"];
        [student setValue:@"yuwen" forKeyPath:@"course.courseName"];
        [student setValue:@"88" forKey:@"point"];
       
        Student* student1 = [[Student alloc] init];
        [student1 setValue:@"78" forKey:@"point"];
       
        Student* student2 = [[Student alloc] init];
        [student2 setValue:@"98" forKey:@"point"];
       
        Student* student3 = [[Student alloc] init];
        [student3 setValue:@"88" forKey:@"point"];
       
        NSArray* array = [NSArray arrayWithObjects:student1,student2,student3, nil];
        [student setValue:array forKey:@"otherStudent"];
       
        NSLog(@"--------------------------------%@",[student valueForKeyPath:@"otherStudent.@count"]);
        NSLog(@"--------------------------------%@",[student valueForKeyPath:@"otherStudent.@max.point"]);
        NSLog(@"---------------------------------%@",[student valueForKeyPath:@"otherStudent.@avg.point"]);
        NSLog(@"---------------------------------%@",[student valueForKeyPath:@"otherStudent.@min.point"]);

        NSLog(@"%@",student);
  
        [course release];
        [student release];
        [student1 release];
        [student2 release];
        [student3 release];
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值