KVC和KVO
AppDelegate.h
// // AppDelegate.h // KVCAndKVO // // Created by Ysfox on 14-8-1. // Copyright (c) 2014年 Ysfox. All rights reserved. // #import <UIKit/UIKit.h> @class ProductItem; @interface AppDelegate : UIResponder <UIApplicationDelegate>{ ProductItem *item0; } @property (strong, nonatomic) UIWindow *window; @property(nonatomic,retain)NSArray *array; @end
AppDelgate.m
// // AppDelegate.m // KVCAndKVO // // Created by Ysfox on 14-8-1. // Copyright (c) 2014年 Ysfox. All rights reserved. // #import "AppDelegate.h" #import "ProductItem.h" @implementation AppDelegate @synthesize array = _array; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; //获取到plist文件的路径 NSString *path = [[NSBundle mainBundle]pathForResource:@"Data" ofType:@"plist"]; //获取到plist文件中所有字典 NSArray *dicArr = [NSArray arrayWithContentsOfFile:path]; //初始化成员数组 NSMutableArray* ProductArr = [[NSMutableArray alloc]init]; //遍历数组,将数据通过kvo的方式添加到数据模型中去 for (NSDictionary *dic in dicArr) { ProductItem *item = [[ProductItem alloc]init]; [item setValuesForKeysWithDictionary:dic]; [ProductArr addObject:item]; } //可以通过KVO去获得数组中所有的数据模型的NSNumber类型成员的平均值@avg,最大值@max,最小值@min,总和@sum NSNumber *max = [ProductArr valueForKeyPath:@"@max.NumRatings"]; NSLog(@"%@",max); //观察一个数据模型的Price的改变 item0 = ProductArr[0]; [self addObserver:self forKeyPath:@"item0.Price" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:@"监听成员字段的属性被改变"]; item0.Price = @"$88.8"; //观察一个成员数组的改变 self.array = @[@"a",@"b",@"c"]; [self addObserver:self forKeyPath:@"array" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:@"监听成员数组改变"]; self.array = @[@"f",@"g",@"h"]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; } -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ if ([keyPath isEqualToString:@"item0.Price"]) { NSLog(@"object:%@",object); //object是修改后的对象 NSLog(@"change:%@",change); //change是新旧值的的字典 NSLog(@"context:%@",context); //context是传递的文本内容 } else if ([keyPath isEqualToString:@"array"]){ NSLog(@"object:%@",object); NSLog(@"change:%@",change); NSLog(@"context:%@",context); } } - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } @end
ProductItem.h
// // ProductItem.h // KVCAndKVO // // Created by Ysfox on 14-8-1. // Copyright (c) 2014年 Ysfox. All rights reserved. // #import <Foundation/Foundation.h> #import "Abc.h" @interface ProductItem : NSObject @property(nonatomic,copy)NSString *Publisher; @property(nonatomic,copy)NSString *Name; @property(nonatomic,strong)NSNumber *NumRatings; @property(nonatomic,strong)NSNumber *Rating; @property(nonatomic,strong)NSString *Price; @property(nonatomic,strong)NSString *Icon; @property(nonatomic,strong)Abc *a; @end
ProductItem.m
// // ProductItem.m // KVCAndKVO // // Created by Ysfox on 14-8-1. // Copyright (c) 2014年 Ysfox. All rights reserved. // #import "ProductItem.h" @implementation ProductItem @synthesize Publisher=_Publisher; @synthesize Name = _Name; @synthesize NumRatings = _NumRatings; @synthesize Price = _Price; @synthesize Icon = _Icon; @synthesize a = _a; -(void)setValue:(id)value forUndefinedKey:(NSString *)key{ NSLog(@"UndefinedKey:%@",key); if ([key isEqualToString:@"a"]) { NSLog(@"多余的参数"); } } @end
Abc.h
// // Abc.h // KVCAndKVO // // Created by Ysfox on 14-8-2. // Copyright (c) 2014年 Ysfox. All rights reserved. // #import <Foundation/Foundation.h> @interface Abc : NSObject @property(nonatomic,copy)NSString *abc; @end
Abc.m
// // Abc.m // KVCAndKVO // // Created by Ysfox on 14-8-2. // Copyright (c) 2014年 Ysfox. All rights reserved. // #import "Abc.h" @implementation Abc @end
Data.plist
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <array> <dict> <key>Publisher</key> <string>Super Sportz, Inc.</string> <key>Name</key> <string>Baseball</string> <key>NumRatings</key> <integer>106</integer> <key>Rating</key> <real>3.5</real> <key>Price</key> <string>$2.98</string> <key>Icon</key> <string>Baseball.png</string> </dict> <dict> <key>Publisher</key> <string>General Specifics, Inc.</string> <key>Name</key> <string>Blocks</string> <key>NumRatings</key> <integer>114</integer> <key>Rating</key> <real>4.5</real> <key>Price</key> <string>$0.99</string> <key>Icon</key> <string>Blocks.png</string> </dict> <dict> <key>Publisher</key> <string>Gameitoids, Inc.</string> <key>Name</key> <string>Checkers</string> <key>NumRatings</key> <integer>87</integer> <key>Rating</key> <integer>4</integer> <key>Price</key> <string>Free</string> <key>Icon</key> <string>Checkers.png</string> </dict> <dict> <key>Publisher</key> <string>Foodies, Inc.</string> <key>Name</key> <string>Dinner</string> <key>NumRatings</key> <integer>254</integer> <key>Rating</key> <real>3</real> <key>Price</key> <string>$4.98</string> <key>Icon</key> <string>Dinner.png</string> </dict> <dict> <key>Publisher</key> <string>Super Sportz, Inc.</string> <key>Name</key> <string>Football</string> <key>NumRatings</key> <integer>422</integer> <key>Rating</key> <real>3</real> <key>Price</key> <string>$1.99</string> <key>Icon</key> <string>Football.png</string> </dict> <dict> <key>Publisher</key> <string>Math Nutz</string> <key>Name</key> <string>MathGraph</string> <key>NumRatings</key> <integer>45</integer> <key>Rating</key> <real>4</real> <key>Price</key> <string>$0.99</string> <key>Icon</key> <string>MathGraph.png</string> </dict> </array> </plist>

浙公网安备 33010602011771号