首页 > 代码库 > KVC
KVC
#import "ViewController.h"
#import "Student.h"
#import "Book.h"
#import "News.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//键-值编码是一种间接访问属性的机制,使用字符串来表示属性而不是通过方法或实例变量
//1. @"name" -> _name
//2. @"name" -> name
Student *student = [[Student alloc] init];
//[student study];
[student setValue:@10 forKey:@"age"];
[student setValue:@11 forKey:@"_age"];
[student setValue:@"ZhangSan" forKey:@"name"];
[student setValue:@"Lisi" forKey:@"_name"];
NSLog(@"%@",[student valueForKey:@"name"]);
[student introduce];
//NSLog(@"____");
Book *book = [[Book alloc] init];
[student setValue:book forKey:@"book"];
book.title = @"abc";
[student introduce];
[student setValue:@"西游记" forKeyPath:@"book.title"];
NSLog(@"%@",[student valueForKeyPath:@"book.title"]);
[student introduce];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObject:@"--sdfd--" forKey:@"name"];
NSLog(@"%@", dict);
[dict setObject:@"sjdlsd" forKey:@"name"];
NSLog(@"%@",dict);
[dict setValue:@"sdfks" forKey:@"name"];
NSLog(@"%@",dict);
[student setValuesForKeysWithDictionary:@{@"name":@"sjfls",@"age":@12}];
[student introduce];
//从主包中获取文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"json" ofType:@"txt"];
NSLog(@"%@",path);
NSString *jsonStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",jsonStr);
NSDictionary *dict1 = [NSJSONSerialization JSONObjectWithData:[jsonStr dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",dict1);
NSLog(@"%@",dict1[@"Name"]);//不方便,没有类型检查
News *news = [News new:dict1];
NSLog(@"%@",news.Name);
//KVC还能对集合做运算,比如想得到所有学生的个数、所有学生的年龄总和、所有学生的平均年龄、年龄的最大值和年龄的最小值
Student *stu = [Student new];
//stu.age = 10;
Student *stu1 = [Student new];
stu1.age = 11;
Student *stu2 = [Student new];
stu2.age = 12;
Student *stu3 = [Student new];
stu3.age = 13;
Student *stu4 = [Student new];
stu4.age = 14;
Student *stu5 = [Student new];
stu5.age = 13;
NSArray *arr = @[stu1,stu2,stu3,stu4,stu5];
NSLog(@"%@",arr);
[stu setValue:arr forKey:@"allStudents"];
NSLog(@"%@",[stu valueForKeyPath:@"allStudents.@count"]);
NSLog(@"%@",[stu valueForKeyPath:@"allStudents.@max.age"]);
NSLog(@"%@",[stu valueForKeyPath:@"allStudents.age"]);
//在使用@distinctUnionOfObjects后,发现效果是消除重复的价年龄
NSLog(@"%@",[stu valueForKeyPath:@"allStudents.@distinctUnionOfObjects.age"]);
NSLog(@"%@",[stu valueForKeyPath:@"allStudents.age"]);
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Student.h:
#import <Foundation/Foundation.h>
@interface Student : NSObject
{
NSString *name;
NSString *_name;
int _age;
NSArray *allStudents;
}
@property (nonatomic,assign) int age;
- (void) introduce;
@end
Student.m:
#import "Student.h"
#import "Book.h"
@interface Student ()
{
Book *_book;
}
@end
@implementation Student
- (void) introduce
{
NSLog(@"%i--%@--%@",_age,name,_name);
NSLog(@"%@----%@",_book,_book.title);
}
@end
Book.h:
@interface Book : Student
@property (nonatomic,copy) NSString *title;
@end
Book.m:
#import "Book.h"
@implementation Book
@end
News.h:
#import <Foundation/Foundation.h>
@interface News : NSObject
@property (nonatomic,copy) NSString *Name,*Pic,*Type,*url;
- (instancetype) initWithDictionary:(NSDictionary *)dict;
+ (instancetype) new:(NSDictionary *)dict;
@end
News.m:
@implementation News
- (instancetype) initWithDictionary:(NSDictionary *)dict
{
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+ (instancetype) new:(NSDictionary *)dict
{
return [[[self class] alloc] initWithDictionary:dict];
}
@end
KVC