gpt4 book ai didi

objective-c - 当 insertObject :inAtIndex: is called? 时是否应该通知观察者

转载 作者:太空狗 更新时间:2023-10-30 03:59:50 26 4
gpt4 key购买 nike

我有一个绑定(bind)到数组 Controller 的模型。我需要能够直接更新模型并将有关更新的通知发送到阵列 Controller 。在我的搜索中,我发现我可以通过使用 mutableArrayValueForKey: 来完成此操作。在我的模型上并通过返回的 NSMutableArray 进行更新。

我还发现了一些引用资料,使我相信如果我实现并使用符合 KVC 的 getter 和可变索引访问器,我也可以更新模型并发送通知。在我实现的代码中

-countOf<Key>:
-objectIn<Key>AtIndex:
-insertObject:in<Key>AtIndex:
-removeObjectFrom<Key>AtIndex:

调用 insertObject:in<Key>AtIndex:没有导致我的观察员被通知。下面的代码是我可以想出的最小的代码来测试我正在尝试做的事情。

#import <Foundation/Foundation.h>

@interface ModelAndObserver : NSObject {
NSMutableArray *theArray;
}

@property(retain)NSMutableArray *theArray;

- (NSUInteger)countOfTheArray;
- (NSString *)objectInTheArrayAtIndex:(NSUInteger)index;
- (void)insertObject:(NSString*) string inTheArrayAtIndex:(NSUInteger)index;
- (void)removeObjectInTheArrayAtIndex:(NSUInteger)index;
@end

@implementation ModelAndObserver
@synthesize theArray;

- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
NSLog(@"theArray now has %d items", [theArray count]);
}

- (NSUInteger)countOfTheArray
{
return [theArray count];
}

- (NSString *)objectInTheArrayAtIndex:(NSUInteger)index
{
return [theArray objectAtIndex:index];
}

- (void)insertObject:(NSString*) string inTheArrayAtIndex:(NSUInteger)index
{
[theArray insertObject:string atIndex:index];
}

- (void)removeObjectInTheArrayAtIndex:(NSUInteger)index
{
[theArray removeObjectAtIndex:index];
}

@end


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

ModelAndObserver *mao = [[ModelAndObserver alloc] init];

[mao addObserver:mao
forKeyPath:@"theArray"
options:0
context:@"arrayChanged"];

// This results in observeValueForKeyPath... begin called.
[mao setTheArray:[NSMutableArray array]];

// This results in observeValueForKeyPath... begin called.
[[mao mutableArrayValueForKey:@"theArray"] addObject:@"Zero"];

// These do not results in observeValueForKeyPath...
// begin called, but theArray is changed.
[mao insertObject:@"One" inTheArrayAtIndex:1];
[mao insertObject:@"Two" inTheArrayAtIndex:2];
[mao insertObject:@"Three" inTheArrayAtIndex:3];

// This results in observeValueForKeyPath... begin called.
[[mao mutableArrayValueForKey:@"theArray"] addObject:@"Four"];


[mao removeObserver:mao forKeyPath:@"theArray"];
[mao release];
[pool drain];
return 0;
}

当我运行这段代码时,我得到以下输出:

2011-02-05 17:38:47.724 kvcExperiment[39048:a0f] theArray now has 0 items2011-02-05 17:38:47.726 kvcExperiment[39048:a0f] theArray now has 1 items2011-02-05 17:38:47.727 kvcExperiment[39048:a0f] theArray now has 5 items

我原以为会看到另外三条日志消息,说明 theArray 现在有 2、3 或 4 个项目。我以为电话insertObject:inTheArrayAtIndex应该通知观察服务器 theArray 已更改,但在我的代码中它没有。

我是不是觉得很困惑 insertObject:inTheArrayAtIndex应该导致向 theArray 的观察者发送通知?或者,我在实现过程中遗漏了什么吗?感谢您的帮助。

最佳答案

那是因为你没有同时实现insert和remove方法。

“什么!”,你说。 “我也是!”

不,不完全是。差不多,但你有一个词错了:它是 removeObjectFromTheArrayAtIndex:

应用该修复后,所有六项更改都会导致观察者通知。

关于objective-c - 当 insertObject :in<Key>AtIndex: is called? 时是否应该通知观察者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4910266/

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