collections - Objective C @property for to-many relationships -


i've had various cases objective-c class has property needs collection class (nsarray usually). there standard way implement this? great able use @synthesize set up. declare property nsmutablearray , @synthesize that, doesn't allow me enforce types of objects can placed collection, nor prevent client code modifying array. typically this:

@property(nonatomic, readonly) nsarray *widgets;  - (void)addwidget:(widget*)widget; - (void)removewidget:(widget*)widget; ... 

the collection implemented nsmutablearray, nsarray containing current contents passed caller. seems lot of coding must common scenario. more coding needs done in order set key-value observing.

am missing something, or work set collection property?

you can prevent client code modifying array declaring property nsarray, using nsmutablearray storage mechanism. @property , @synthesize directives still work.

there isn't way ensure type-safety of objects returned array, objective-c programmers never worry that. not common concern. if want ensure objects of type go , come out of array, you're going have write wrapper class nsarray so.


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -