objective-c - NSMutableDictionary 和新项目插入 KVO

标签 objective-c cocoa nsmutabledictionary key-value-observing key-value-coding

我先描述一下我的问题。我有一个包含 NSMutableDictionary ivar 的类。有一个线程会将新对添加到该字典中 - 在编写应用程序时,我无法获得可用键的完整列表。所有这些对的最新列表必须显示在 TableView 中。 我准备了一个小的概念验证应用程序,其中在 xib 文件中创建了一个字典 Controller ,并且其内容与字典 ivar 绑定(bind)。问题是 TableView 仅显示字典内容的初始集。插入后不刷新。

在我的概念验证应用程序 AppController.h 中,如下所示:

#import <Foundation/Foundation.h>

@interface AppController : NSObject

@property (strong) NSMutableDictionary *dictionary;

- (NSString *)randomString;
- (IBAction)addRandomPair:(id)sender;

@end

以及实现文件内容:

#import "AppController.h"

@implementation AppController

@synthesize dictionary = _dictionary;

- (id)init {
    self = [super init];
    if (self) {
        _dictionary = [[NSMutableDictionary alloc] init];
        [_dictionary setValue:@"Aa" forKey:@"A"];
        [_dictionary setValue:@"Bb" forKey:@"B"];
    }
    return self;
}

- (NSString *)randomString
{
    NSMutableString *aString = [[NSMutableString alloc] init];
    for (int i = 0; i < 3; i++) {
        NSUInteger r = random() % ('z' - 'a');
        [aString appendString:[NSString stringWithFormat:@"%c", ('a' +r)]];
    }
    return aString;
}

- (IBAction)addRandomPair:(id)sender
{
    [self.dictionary setValue:[self randomString] forKey:[self randomString]];
    NSLog([self.dictionary description]);
}

@end

字典 Controller 内容绑定(bind)到应用程序 Controller ,模型键路径设置为“self.dictionary”, TableView 中的列内容绑定(bind)到字典 Controller ,模型键路径设置为相应的键和值。在此概念证明应用程序按钮中,单击添加一个新对(addRandomPair:操作)。

我在使用 NSMutableArray 和数组 Controller 时遇到了类似的问题,但是我可以通过向包含数组 ivar(此类中的命名数据)的类添加以下一对方法来解决该问题:

- (void)insertObject:(NSString *)object inDataAtIndex:(NSUInteger)index;
- (void)removeObjectFromDataAtIndex:(NSUInteger)index;

是否可以向保存字典的类(AppController)添加一些其他方法,以获取有关新插入的通知?或者也许有更好的解决方案来解决我的问题?

更新

我发现实现以下一组访问器可以使字典 Controller 收到有关新项目的通知:

- (void)addDictionaryObject:(NSString *)object;
- (void)removeDictionaryObject:(NSString *)object;

问题是 addDictionaryObject: 只有一个参数,字典需要类似 addDictionaryObject:forKey: 的东西。有什么想法吗?

更新2

除了使用手动更改通知之外,我没有看到任何其他解决方案 - 在本例中 addRandomPair: 方法如下所示:

- (IBAction)addRandomPair:(id)sender
{
    [self willChangeValueForKey:@"dictionary"];
    [self.dictionary setValue:[self randomString] forKey:[self randomString]];
    [self didChangeValueForKey:@"dictionary"];
    NSLog([self.dictionary description]);
}

它有效,但我仍然有点不确定,因为字典本身不会改变,但它的内容会改变。 使用手动更改通知是正确的方法吗?

最佳答案

来自Key-Value Coding methods文档中,To-Many 属性仅支持 NSMutableArrayNSMutableSet。由于 KVC 已经可以使用键,因此支持 NSMutableDictionary 是多余的,因为它实际上是 setValue:forKey: 已经做的事情。

如果您确实希望在一次调用中实现这一点,则可以覆盖 setValue:forKeyPath:

关于objective-c - NSMutableDictionary 和新项目插入 KVO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8168762/

相关文章:

objective-c - Doxygen/Objective-C : how can I make constants that are declared outside the class definition show up in the class docs?

objective-c - 在 Typhoon 中创建对象时崩溃

cocoa - 使用分布式对象出售对象并同步访问

objective-c - 匿名类别或 "private"类别是否相同?

ios - 如何在 swift 2.0 中从 Dict 中删除重复值

iphone - iOS 上的页面 curl

objective-c - Cmd+Shift+4 Cocoa/Obj-C 中的模拟错误

macos - 如何查找磁盘卷的格式

iphone - 如何释放返回的对象?

ios - 从 NSMutableDictionary 中获取对象并将其显示在 UITableView 中