将对象添加到 NSDictionaryController

发布于 2024-09-01 01:08:11 字数 221 浏览 4 评论 0原文

我有一个绑定到 NSDictionaryController 的 NSTableView。我已经通过界面生成器设置了它和内容字典。我的问题是,如何将对象添加到字典中,以便控制器自动看到它并将其显示在表中。

我在有关 NSArrayController 的示例中看到,您通过控制器而不是实际数组添加新对象,以便控制器看到更改。但是,我没有看到使用字典控制器执行此操作的类似方法...

感谢您的帮助。

I have an NSTableView which is bound to an NSDictionaryController. I have set this and the content dictionary through the interface builder. The question I have is, how do I add objects to the dictionary in a manner that the controller will see it automatically and display it in the table.

I've seen in examples about NSArrayController that you add the new objects through the controller rather than the actual array so the controller sees the change. However, I don't see a similar way of doing this with the dictionarycontroller...

Thanks for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

最美的太阳 2024-09-08 01:08:12

出色的!针对 Swift3 修改:

    // Use NSDictionaryControllerKeyValuePair Protocol setKey
    let temp = myDictionaryController.newObject()
    temp.setValue(name, forKey: "key")
    temp.setValue(list, forKey: "value")
    myDictionaryController.addObject(temp)

Excellent! modified for Swift3:

    // Use NSDictionaryControllerKeyValuePair Protocol setKey
    let temp = myDictionaryController.newObject()
    temp.setValue(name, forKey: "key")
    temp.setValue(list, forKey: "value")
    myDictionaryController.addObject(temp)
﹎☆浅夏丿初晴 2024-09-08 01:08:12

杰森的答案可以简化:

        NSDictionaryControllerKeyValuePair *c = myDictionaryController.newObject;
        c.key   = @"foo";
        c.value = @"bar";
        [myDictionaryController addObject:c];

Jason's answer can be simplified:

        NSDictionaryControllerKeyValuePair *c = myDictionaryController.newObject;
        c.key   = @"foo";
        c.value = @"bar";
        [myDictionaryController addObject:c];
极度宠爱 2024-09-08 01:08:11

好吧,我最终弄清楚了这一点。我认为由于数组控制器镜像了数组的使用,所以字典控制器也会做同样的事情,但事实并非如此。这个过程有点不同......

所以如果你有一个字典控制器,你想添加值,以便它可以更新任何绑定到它的东西,你使用以下过程:

//Creates and returns a new key-value pair to represent an entry in the 
//content dictionary.
id newObject = [dictController newObject];
//Use NSDictionaryControllerKeyValuePair Protocol setKey 
[newObject setKey:@"Key"];
//Use NSDictionaryControllerKeyValuePair Protocol setValue
[newObject setValue:@"value"];
//Add the object to the controller
[dictController addObject:newObject];

这将有效,更新字典控制器并添加内容字典的值。在这种情况下您不能使用 setValue:ForKey 因为这不是 NSDictionaryControllerKeyValuePair 协议中实现的方法。

我希望这可以帮助其他使用这些控制器的人!

Well I ended up figuring this out. I figured since the arraycontroller mirrored the use of an array that a dictionarycontroller would do the same but it is not so. The process is a little bit different...

So if you have a dictionarycontroller that you would like to add values to so it can update anything binding to it you use the following process:

//Creates and returns a new key-value pair to represent an entry in the 
//content dictionary.
id newObject = [dictController newObject];
//Use NSDictionaryControllerKeyValuePair Protocol setKey 
[newObject setKey:@"Key"];
//Use NSDictionaryControllerKeyValuePair Protocol setValue
[newObject setValue:@"value"];
//Add the object to the controller
[dictController addObject:newObject];

This will in effect, update both the dictionarycontroller and add the value to the content dictionary. You cannot use setValue:ForKey in this case because this is not a method implemented in the NSDictionaryControllerKeyValuePair Protocol.

I hope this can help others using these controllers!

漫雪独思 2024-09-08 01:08:11

jasons 的答案对我不起作用,因为在我的实现中,它在 [newObject setValue:@"value"] 行上遇到了错误,说发现了多个结果不匹配的方法,并且我找不到任何其他方法可以按预期添加新条目。

经过大量搜索和尝试,我想出了这个解决方案:
每次 dictController 需要更新时只需更新绑定,如下所示:

[dictController bind:NSContentDictionaryBinding toObject:self withKeyPath:@"nameOfMyDictionary" options:nil];

因此在我的代码中,我将项目添加到我的字典中,并且在显示包含字典内容的表格之前,我重置了绑定。这可能是一个肮脏的解决方案,但它对我有用。

jasons answer didn't work for me because in my implementation it ran into an error on the line [newObject setValue:@"value"], saying that multiple methods were found with mismatched results and i couldn't find any other method that would add the new entry as intended.

after a lot of searching and trying i came up with this solution:
just renew the binding everytime the dictController needs to be updated, like this:

[dictController bind:NSContentDictionaryBinding toObject:self withKeyPath:@"nameOfMyDictionary" options:nil];

so throughout my code i add items to my dictionary, and just before showing the table with the contents of my dictionary i reset the binding. this is probably a dirty solution, but it worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文