gpt4 book ai didi

ios - cocoa 键值观察字典中的键/条目

转载 作者:IT王子 更新时间:2023-10-29 08:19:42 25 4
gpt4 key购买 nike

是否可以观察字典中的特定键?如果可以,我该怎么做?

最佳答案

是的(尽管只有观察 NSMutableDictionary 才有意义)。

@interface Foo : NSObject @end
@implementation Foo

- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
NSLog(@"observing: -[%@ %@]", object, keyPath);
NSLog(@"change: %@", change);
}

@end

int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

Foo * f = [[Foo alloc] init];

NSMutableDictionary * d = [NSMutableDictionary dictionary];
[d addObserver:f forKeyPath:@"foo" options:0 context:NULL];
[d setObject:@"bar" forKey:@"foo"];
[d removeObjectForKey:@"foo"];
[d removeObserver:f forKeyPath:@"foo"];
[f release];

[pool drain];
return 0;
}

日志:

2010-12-21 17:39:53.758 EmptyFoundation[94589:a0f] observing: -[{
foo = bar;
} foo]
2010-12-21 17:39:53.764 EmptyFoundation[94589:a0f] change: {
kind = 1;
}
2010-12-21 17:39:53.765 EmptyFoundation[94589:a0f] observing: -[{
} foo]
2010-12-21 17:39:53.765 EmptyFoundation[94589:a0f] change: {
kind = 1;
}

关于ios - cocoa 键值观察字典中的键/条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4505444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com