是否可以使用 Cocoa 的 Bindings 将可编辑的 NSTableView 连接到 NSMutableArray?

发布于 2024-07-18 02:23:47 字数 552 浏览 5 评论 0原文

从我能收集到的零碎信息看来,这个问题的答案是“不”,所以我将广泛地解释我在(可能的)情况下试图实现的目标,我正在努力解决这个问题一颗螺丝。

我在我的应用程序中拥有的是要读入的有效文件扩展名列表,我正在通过目录进行递归。 我希望这是一个首选项,所以我有以下内容:

NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:[NSMutableArray arrayWithObjects:@"pdf", @"rtf", @"txt", nil] forKey:@"validExtensions"];
[[NSUserDefaults standardUserDefaults] registerDefaults:dic];

并且我将 NSTableView 绑定到 NSArrayController,该 NSArrayController 绑定到共享用户默认控制器...

这是否是拥有用户可自定义的完全错误的方法有效扩展名列表? 我是否误解了 NSUserDefaults 的作用?

It seems from the bits and pieces I can scrape together that the answer to this one will be "no", so I'll broadly explain what I'm trying to achieve in (the likely) case that I'm trying to hammer in a screw.

What I have in my app is a list of valid file extensions to read in, which I'm recursing through a directory for. I want this to be a preference, so I've got the following:

NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:[NSMutableArray arrayWithObjects:@"pdf", @"rtf", @"txt", nil] forKey:@"validExtensions"];
[[NSUserDefaults standardUserDefaults] registerDefaults:dic];

and I'm binding the NSTableView to an NSArrayController which is bound to the Shared User Defaults controller...

Is this completely the wrong approach to having a user-customisable list of valid extensions? Have I misunderstood the role of NSUserDefaults?

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

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

发布评论

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

评论(1

云胡 2024-07-25 02:23:47

标题中提出的问题的答案是“是”...但不幸的是,这对您没有帮助,因为实际 NSUserDefaults 后备存储中数据结构的可变性不受您传递的对象的可变性控制与 registerDefaults: 一起使用。

对于本身就是结构的首选项值,为了更改它们,您必须读取旧结构,为您需要修改的任何部分制作可变副本,更改它,然后将该结构设置为新值。 不幸的是,这不太适合绑定,因此您需要一个粘合层,将绑定数组中的更改镜像到首选项中。

The answer to the question posed in the title is "Yes"... but unfortunately that doesn't help you, because the mutability of the data structures in the actual NSUserDefaults backing store isn't controlled by the mutability of the objects you pass in with registerDefaults:.

For preference values that are themselves structures, in order to change them you have to read the old structure, make a mutable copy of whatever portion you need to modify, alter that, and then set that structure as the new value. Unfortunately that's not well-suited to bindings, so you' d need a glue layer that mirrors changes in a bound array into the prefs.

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