如何在 Cocoa 中创建大型决策树
我正在开发一个 iOS 应用程序,我需要用户能够通过决策树选择某个报告的主题(使用 UITableView
通过可用选项前进)。 我正在尝试找到一种订购所有可用选项的好方法,大约有 5 层,其中一些层有 10 多个项目。
- 橱柜
- 木制
- 黑色
- 闪亮
- 不闪亮
- 等等
- 黑色
- 棕色
- 等等
- 白色
- 等等
- 木制
- 钢
- 等等
- 面料
- 等等
任何人都可以建议某种变量类型来执行此操作吗? 我考虑过使用 NSMutableArray 并用指向其他数组等的指针填充它,但我想我最终会得到一大堆指针,而只有最后才真正有 返回了 NSStrings
(如果这有意义的话)。
I'm working on an iOS app and I need the user to be able select a subject of a certain report through a decision tree (using the UITableView
to advance trough the available options).
I'm trying to find a good way of order all the available options, there are about 5 layers and 10+ items for some of the layers.
- Cabinets
- Wooden
- Black
- shiny
- not shiny
- etc
- Black
- Brown
- etc
- White
- etc
- Wooden
- Steel
- etc
- Fabric
- etc
Can anyone maybe advice a certain variable type to do this with?
I've thought about using a NSMutableArray
and populating it with pointers to other arrays etc, but I figured I'd end up with a whole bunch of pointers where only in the end there are actually NSStrings
returned (if that makes any sense).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的建议是使用 plists 让你的生活更轻松。 Plist 可以轻松读入 NSDictionary 和 NSArray 对象;如果需要,您还可以轻松地将
NSArray
和NSDictionary
持久保存到 plist 中。如果 plist 内容是支持 NSCoding 协议的类型(您的示例数据似乎就是这种情况),则这是正确的。您的情况的主要优点是您可以使用 Xcode plist 编辑器创建 plist 文件(这只是一个 XML 文件),然后将其读入内存并作为 Obj-C 对象访问其内容,而不必担心必须自己创建所有
NSArrays
来保存指针。这是正确的,这是你可以走的路。您可以手动构建数组,或者使用 plist 编辑器,正如我上面提到的。
否则,您可以寻找一些适合表示决策树的专门数据结构实现。还可以看看 CHDataStructures,它是 Obj-C 中数据结构的集合。
My suggestion is using plists to make your life easier. Plists can be read easily into
NSDictionary
andNSArray
objects; you can also persistNSArray
andNSDictionary
easily into plists, if you need it. This is true if the plist content are types that support theNSCoding
protocol (which seems to be the case for your example data).The main advantage in your case is that you could use Xcode plist editor to create your plist file (which is just an XML file) and then read it into memory and access its content as Obj-C objects, without worrying about having to create yourself all the
NSArrays
to hold the pointers.This is correct and it is a way you can go. You could build your arrays manually, or use the plist editor, as I mentioned above.
Otherwise you could look for some specialized data structure implementation apt to represent a decision tree. Have also a look at CHDataStructures, which is a collection of data structures in Obj-C.