Dav*_*vid 7 cocoa key-value-observing
我需要一些帮助来尝试在复杂的对象层次结构上理解KVO.让我设置一下场景.MyClass对象具有包含MyPerson对象的可变数组属性.我想观察MyClass的myPeople属性的变化.此外,我还想观察MyPerson对象中包含的所有属性.这是类定义.
@interface MyClass:NSObject
{
NSMutableArray *myPeople;
}
@property(nonatomic, retain)NSMutableArray *myArray;
@end
Run Code Online (Sandbox Code Playgroud)
这是MyPerson对象,
@interface MyPerson:NSObject
{
NSString *myName;
NSString *myLastName;
}
@property(nonatomic, retain)NSString *myName;
@property(nonatomic, retain)NSString *myLastName;
@end
Run Code Online (Sandbox Code Playgroud)
以下列方式观察我感兴趣的属性是否正确?
MyClass *myClass = [[MyClass alloc] init]; //myPeople is filled with myPerson objects
MySchool *mySchool = [[MySchool alloc] init];
[myClass addObserver:mySchool
forKeyPath:@"myPeople"
options:NSKeyValueObservingOptionNew
context:NULL];
[myClass addObserver:mySchool
forKeyPath:@"myPeople.myName"
options:NSKeyValueObservingOptionNew
context:NULL]; //I am unsure about this one
[myClass addObserver:mySchool
forKeyPath:@"myPeople.myLastName"
options:NSKeyValueObservingOptionNew
context:NULL]; //I am unsure about this one
Run Code Online (Sandbox Code Playgroud)