Object-C 学习笔记(三十四)--- KVC(基本运算)

上一节提及到KVC的基本知识以及相应历程,这里继续使用.

实例:

//
//  Computer.m
//  KVC_KVO
//
//  Created by 5016 on 13-12-10.
//  Copyright (c) 2013年 dradon. All rights reserved.
//

#import "Computer.h"

@implementation Computer

@end


//
//  Computer.h
//  KVC_KVO
//
//  Created by 5016 on 13-12-10.
//  Copyright (c) 2013年 dradon. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Computer : NSObject
{
    NSString *_name;
    NSInteger _price;
}

@end
//
//  Student.h
//  KVC_KVO
//
//  Created by 5016 on 13-12-10.
//  Copyright (c) 2013年 dradon. All rights reserved.
//

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

@interface Student : NSObject
{
    NSString *_name;
    NSInteger _age;
    Computer *_computer;
    NSArray *_computers;
}

-(id)initWithName:(NSString *) name
           andAge:(NSInteger) age;
@end

//
//  Student.m
//  KVC_KVO
//
//  Created by 5016 on 13-12-10.
//  Copyright (c) 2013年 dradon. All rights reserved.
//

#import "Student.h"

@implementation Student


-(id)initWithName:(NSString *) name
           andAge:(NSInteger) age
{
    if(self = [super init])
    {
        /*以前的写法
        self->_name = name;
        self->_age = age;
         */
        //KVC写法
        [self setValue:name forKey:@"_name"];
        [self setValue:[NSNumber numberWithInteger:age] forKey:@"_age"];
    }
    
    return self;
}

-(void)dealloc
{
    [_name release];
    [super dealloc];
}

@end

//
//  AppDelegate.m
//  KVC_KVO
//
//Created  5016 on 13-12-10.
//   年 . All rights reserved.
//
#import "AppDelegate.h"
#import "Student.h"

@implementation AppDelegate

- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    //KCV设置值
    Student *stu = [[Student alloc] init];
    [stu setValue:@"小明" forKey:@"_name"];
    [stu setValue:[NSNumber numberWithInteger:22] forKey:@"_age"];
    //KVC取值
    NSString *name = [stu valueForKey:@"_name"];
    NSString *age = [stu valueForKey:@"_age"];
    NSLog(@"name = %@ ,age = %@",name,age);
    
    Student *stu1 = [[Student alloc] initWithName:@"小红" andAge:18];
    NSLog(@"name = %@ age = %@",[stu1 valueForKey:@"_name"],[stu1 valueForKey:@"_age"]);
    [stu release];
    [stu1 release];
    
    
    //KVC通过路径访问
    Student *stu3 = [Student alloc];
    Computer *computer = [[Computer alloc] init];
    [computer setValue:@"MAC.mini" forKey:@"_name"];
    NSLog(@"com_name = %@",[computer valueForKey:@"_name"]);
    
    //路径取值
    [stu3 setValue:computer forKey:@"_computer"];
    NSString *com_name = [[stu3 valueForKey:@"_computer"] valueForKey:@"_name"];
    NSLog(@"com_name = %@",com_name);
    NSString *com_name1 = [stu3 valueForKeyPath:@"_computer._name"];
    NSLog(@"com_name1 = %@",com_name1);
    
    //KVC一对多的访问
    Computer *MacAri = [[Computer alloc] init];
    Computer *MacPro = [[Computer alloc] init];
    Computer *MacMini = [[Computer alloc] init];

    [MacAri setValue:@"Mac.Ari" forKey:@"_name"];
    [MacPro setValue:@"Mac.Pro" forKey:@"_name"];
    [MacMini setValue:@"Mac.Mini" forKey:@"_name"];
    
    //把对象放入到数组
    NSMutableArray *computerArray = [NSMutableArray arrayWithObjects:MacAri,MacPro,MacMini,nil];
    [stu3 setValue:computerArray forKey:@"_computers"];
    NSLog(@"stu3 所拥有的电脑%@",[stu3 valueForKeyPath:@"_computers._name"]);
    
    
    //必须是基本数据类型才能运算
    [MacAri setValue:[NSNumber numberWithInteger:7988] forKey:@"_price"];
    [MacPro setValue:[NSNumber numberWithInteger:8888] forKey:@"_price"];
    [MacMini setValue:[NSNumber numberWithInteger:5848] forKey:@"_price"];
    
    NSNumber *sum = [stu3 valueForKeyPath:@"_computers.@sum._price"];//sum是求和公式
    NSNumber *min = [stu3 valueForKeyPath:@"_computers.@min._price"];//min求最小值
    NSNumber *max = [stu3 valueForKeyPath:@"_computers.@max._price"];//min求最大值
    NSNumber *avg = [stu3 valueForKeyPath:@"_computers.@avg._price"];//avg求最平均值
    NSNumber *count = [stu3 valueForKeyPath:@"_computers.@count._price"];//count统计
    NSLog(@"sum = %@",sum);
    NSLog(@"min = %@",min);
    NSLog(@"max = %@",max);
    NSLog(@"avg = %@",avg);
    NSLog(@"count = %@",count);
    
    [self.window makeKeyAndVisible];
    return YES;
}

@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值