ios - Declaration under interface in Objective C -
i studying big nerd ranch's objective c programming book. saw code below:
@interface bnremployee : bnrperson { nsmutablearray *_assets; } @property (nonatomic) unsigned int employeeid; @property (nonatomic) unsigned int officealarmcode; @property (nonatomic) nsdate *hiredate; @property (nonatomic, copy) nsarray *assets; -(double)yearsofemployment; -(void)addasset:(bnrasset *)a; -(unsigned int)valueofassets; in code, why declare nsmutablearray *_assets under interface? how different declaring property, , purpose serve?
lastly, see there nsarray *assets in property. same nsmutablearray *_assets?
i'm not sure why did general practice, shouldn't yourself. header file should reserved public interface of class. put things in there callers , users of class need see, properties, methods, , perhaps extern constants.
then question becomes in implementation whether use property or regular instance variable. largely preference based. people declare properties , don't use plain ivars @ all. others use ivars , properties when want declare custom setter/getter variable in question. in latter camp arguable clearer , easier read if property.
edit
i misread code. above stands normally, doing there exposing api different underlying data. editing answer now.
when declare property without custom @synthesize , without having overridden both setter , getter if applicable, underlying variable created underscore in front. doing here returning nsarray in public api ensure internal variable not modified while internally using nsmutablearray.
i in general though, variable declaration (nsmutablearray *_assets;) should still go in implementation file. caller should not need know mutable under hood.
there lot of existing questions touching upon already. here search query number of them: https://stackoverflow.com/search?q=mutable+ivar+immutable+property
Comments
Post a Comment