Note that there are some explanatory texts on larger screens.

plurals
  1. POBetter alternative for "data-only" Objective-C objects?
    primarykey
    data
    text
    <p>I run into design choices like this often and struggle a bit; I'm looking for some other perspectives.</p> <p>I often want to keep lists of, or pass around chunks of state that are basically just sets of values. The values tend to be primitive types: floats, NSTimeIntervals, CGPoints, etc.</p> <p>My first inclination is often to create C structures for these sets of properties, e.g.</p> <pre><code>typedef struct _STATE { float foo; NSTimeInterval elapsed; CGPoint point; } STATE; </code></pre> <p>etc.</p> <p>But C structures don't play nicely with the native Cocoa collection classes (<code>NSArray</code>, <code>NSSet</code>, <code>NSDictionary</code>), and using overmany of them to track lots of state feels like it runs against the grain of rest of my Cocoa-friendly code-- I end up having and directly managing arrays of structs, and passing struct pointers around in messages, etc.</p> <p>On the other hand, since raw performance isn't necessarily critical, I could encode these values into a NSDictionary, wrapping them all in <code>NSValue</code> or <code>NSNumber</code>, but the resulting syntax is hardly terse, and a little fragile, requiring type and name correctness at runtime for both the insert and the lookup:</p> <pre><code>[stateDict setObject:[NSNumber numberWithFloat:foo] forKey:@"bar"]; ... float something = [[stateDict objectForKey:@"bar"] floatValue]; </code></pre> <p>and some types, like NSTimeInterval, are only able to be used with some (arguable) hackery (typecast to double in that case).</p> <p>Finally, I could create data-only container objects, with private member data and only getters/setters. (These would be called "beans" in Java.) These are more terse to access than dictionaries, more Cocoa than structs, but feel like overkill to me, especially if I only need them as "inner classes" that are used for state management internal to a single object type.</p> <p>How do you, great Cocoa programming public, do this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload