【发布时间】:2016-03-17 10:02:50
【问题描述】:
在 Objective-C 中,当使用 Key-Value Observing 时,我有一个带有 accountDomestic 属性和 person 属性的 Bank 类。添加此人以观察 accountDomestic 属性。我在 Bank 类中有一个 static void *bankContext = & bankContext 作为其上下文。但是,在我更改 accountDomestic 属性后,由于 Person 中 -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)contextmethod 中的 context 和 bankContext 不匹配,旧值和新值无法正确显示。
代码如下,首先是银行类:
Bank.h
#import <Foundation/Foundation.h>
#import "Person.h"
static void * const bankContext = &bankContext;
@class Person;
@interface Bank : NSObject
@property (nonatomic, strong) NSNumber* accountDomestic;
@property (nonatomic, strong) Person* person;
-(instancetype)initWithPerson:(Person *)person;
@end
Bank.m
@implementation
-(instancetype)initWithPerson:(Person *)person{
if(self = [super init]){
_person = person;
[self addObserver:_person
forKeyPath:NSStringFromSelector(@selector(accountDomestic))
options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew
context:bankContext];
}
return self;
}
-(void)dealloc{
[self removeObserver:_person forKeyPath:NSStringFromSelector(@selector(accountDomestic))];
}
@end
然后是 Person 类:
Person.h
#import <Foundation/Foundation.h>
#import "Bank.h"
@interface Person : NSObject
@end
Person.m
#import "Person.h"
@implementation Person
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
NSLog(@"context: %p",context);
NSLog(@"bankContext: %p",bankContext);
if(context == bankContext){
if([keyPath isEqualToString:NSStringFromSelector(@selector(accountDomestic))]){
NSString *oldValue = change[NSKeyValueChangeOldKey];
NSString *newValue = change[NSKeyValueChangeNewKey];
NSLog(@"--------------------------");
NSLog(@"accountDomestic old value: %@", oldValue);
NSLog(@"accountDomestic new value: %@", newValue);
}
}
}
@end
最后是 ViewController 类
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
ViewController.m
#import "ViewController.h"
#import "Bank.h"
#import "Person.h"
@interface ViewController ()
@property (nonatomic, strong) Bank *bank;
@property (nonatomic, strong) Person *person;
@property (nonatomic, strong) NSNumber *delta;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.person = [[Person alloc] init];
self.delta = @10;
self.bank = [[Bank alloc] initWithPerson:self.person];
}
- (IBAction)accountDomesticIncreaseButtonDidTouch:(id)sender {
self.bank.accountDomestic = self.delta;
int temp = [self.delta intValue];
temp += 10;
self.delta = [NSNumber numberWithInt:temp];
}
@end
单击按钮后,accountDomestic 的新旧值未显示。可以看到 context 和 bankContext 的值不相等,如下图所示:
有人知道吗?
【问题讨论】:
-
如果你使用一个静态类,那么这个类如果有效地减少为一个单例。为什么要这样限制课程?
-
本文建议nshipster.com/key-value-observing 。作为令牌。如果我不关心不同的实例,只要同一个类,我认为就可以了。关键是为什么contex和bankContext不相等。
-
啊,好吧——我的错。我收回了:)
标签: objective-c key-value-observing