这个算是类型的不匹配,就是把NSNumber类型的赋给字符串了自己还不知情,因为我的是测试,代码如下(viewController.m里面)
- (void)viewDidLoad {
[super viewDidLoad];
bankAccount = [[BankObject alloc] init];
[bankAccount setValue:@"10" forKey:@"account"];
[bankAccount addObserver:self forKeyPath:@"account" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
_myLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 100)];
_myLabel.text = [bankAccount valueForKey:@"account"];
_myLabel.textColor = [UIColor whiteColor];
[self.view addSubview:_myLabel];
_myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_myButton.frame = CGRectMake(200, 300, 100, 50);
_myButton.backgroundColor = [UIColor redColor];
[_myButton addTarget:self action:@selector(changeAccount) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_myButton];
}
打断点,也是程序执行完viewDidLoad后才会崩,察觉不到_myLabel.text = [bankAccount valueForKey:@"account"];这个错误,改成_myLabel.text = [NSString stringWithFormat:@"%@",[bankAccount valueForKey:@"account"]];就可以了