Does using lists of structs make sense in cocoa?

前端 未结 3 1953
旧巷少年郎
旧巷少年郎 2021-01-13 05:32

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 com

3条回答
  •  臣服心动
    2021-01-13 05:46

    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.

    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.

提交回复
热议问题