Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyValueCoding #6

Open
dabing1022 opened this issue Sep 9, 2015 · 2 comments
Open

KeyValueCoding #6

dabing1022 opened this issue Sep 9, 2015 · 2 comments

Comments

@dabing1022
Copy link
Owner

  1. 键值编码在什么情况适用?
  2. 键值编码的弊端
    • 属性路径的拼写:不能依赖智能提示,容易出现拼写错误
    • 不能依赖编译器的检查,运行时才报错崩溃
  3. 所有的键必须以小写字母开头
@dabing1022
Copy link
Owner Author

#import <Foundation/Foundation.h>
@class Course;
@interface Student : NSObject
{
  NSString *name;
  Course *course;
  NSInteger point;
  NSArray *otherStudent;
}
@end

#import "Student.h"
#import "Course.h"
int main(int argc, const char * argv[])
{
    @autoreleasepool {
  Student *student = [[[Student alloc]init ]autorelease];
  [student setValue:@"张三" forKey:@"name"];
  NSString *name = [student valueForKey:@"name"];
  NSLog(@"学生姓名:%@",name);
  [student setValue:@"88" forKey:@"point"];
  NSString *point = [student valueForKey:@"point"];
  NSLog(@"分数:%@", point);
  Student *student1 = [[[Student alloc]init]autorelease];
  Student *student2 = [[[Student alloc]init]autorelease];
  Student *student3 = [[[Student alloc]init]autorelease];
  [student1 setValue:@"65" forKey:@"point"];
  [student2 setValue:@"77" forKey:@"point"];
  [student3 setValue:@"99" forKey:@"point"];
  NSArray *array = [NSArray arrayWithObjects:student1,student2,student3,nil];
  [student setValue:array forKey:@"otherStudent"];
  NSLog(@"其他学生的成绩%@", [student valueForKeyPath:@"otherStudent.point"]);
  NSLog(@"%@个学生", [student valueForKeyPath:@"otherStudent.@count"]);
  NSLog(@"最高成绩:%@", [student valueForKeyPath:@"[email protected]"]);
  NSLog(@"最低成绩:%@", [student valueForKeyPath:@"[email protected]"]);
  NSLog(@"平均成绩:%@", [student valueForKeyPath:@"[email protected]"]);
    }
    return 0;
}

运行打印结果

2012-07-20 16:09:17.101 objectiveC[2857:403]  学生姓名 : 张三

2012-07-20 16:09:17.104 objectiveC[2857:403]  分数 :88

2012-07-20 16:09:17.105 objectiveC[2857:403]  其他学生的成绩 (

65,

77,

99

)

2012-07-20 16:09:17.106 objectiveC[2857:403]  共 3 个学生

2012-07-20 16:09:17.106 objectiveC[2857:403]  最高成绩 :99

2012-07-20 16:09:17.107 objectiveC[2857:403]  最低成绩 :65

2012-07-20 16:09:17.108 objectiveC[2857:403]  平均成绩 :80.333333333333333333333333333333333333

@dabing1022
Copy link
Owner Author

@interface User : NSObject<NSMutableCopying,NSCopying>

@property (nonatomic,copy) NSString *userId;
@property (nonatomic,copy) NSString *name;
@property (nonatomic,assign) NSInteger age;
@property (nonatomic,copy) NSString *sex;

- (id)initWithDictionary:(NSDictionary *)jsonDictionary;
@end

@implementation User

- (id)initWithDictionary:(NSDictionary *)jsonDictionary
{
    self = [super init];
    if(self){
        [self setValuesForKeysWithDictionary:jsonDictionary];
    }
    return self;
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key  {
    if([key isEqualToString:@"id"])
       self.userId = value;
    else
       [super setValue:value forKey:key];
}
"id" : "0",
"name" : "jack",
"age" : 15,
"sex" : "male"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant