问题描述
此问题已经催生出了这one.在可可结构清单工作并不简单。要么使用的NSArray和连接code /德code,或使用C型阵列和失去的NSArray的商品。结构应该是简单的,但需要一个列表时,人们往往会构建一个类来代替。
This question has spawned out of this one. Working with lists of structs in cocoa is not simple. Either use NSArray and encode/decode, or use a C type array and lose the commodities of NSArray. Structs are supposed to be simple, but when a list is needed, one would tend to build a class instead.
在使用结构做的名单有意义的可可?
When does using lists of structs make sense in cocoa?
我知道有关于结构VS班已经有很多的问题,我读过的用户认为它是对每一种语言一样的答案,但至少可可应该有自己特定的答案,这一点,如果仅仅是因为KVC或绑定(彼得对第一个问题的建议)。
I know there are already many questions regarding structs vs classes, and I've read users argue that it's the same answer for every language, but at least cocoa should have its own specific answers to this, if only because of KVC or bindings (as Peter suggested on the first question).
推荐答案
可可有几个常见的是结构,而不是对象:NSPoint,的NSRect,NSRange(和他们的CG同行)
Cocoa has a few common types that are structs, not objects: NSPoint, NSRect, NSRange (and their CG counterparts).
如果有疑问,请按照可可的领先地位。如果你发现自己处理大量的小,主要是数据的对象,你可能想使它们结构,而不是效率。
When in doubt, follow Cocoa's lead. If you find yourself dealing with a large number of small, mostly-data objects, you might want to make them structs instead for efficiency.
使用的NSArray / NSMutableArray里的顶层容器,和包装在NSValue的结构将可能使你的生活变得更加简单。我只会去直C型数组,如果你发现NSArray的是一个性能瓶颈,或者可能如果阵列基本上是只读的。
Using NSArray/NSMutableArray as the top-level container, and wrapping the structs in an NSValue will probably make your life a lot easier. I would only go to a straight C-type array if you find NSArray to be a performance bottleneck, or possibly if the array is essentially read-only.
这篇关于是否使用结构的名单使可可有意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!