开发者

c-style member vars in obj-c

I've noticed that I cannot use the @property / @synthesize for member vars that are arrays in obj-c. For instance the member var int mVar[5] cannot use the @property/@synthesize.

However, I've noticed that I can set these vars simply by not using self.mVar[n] but instead using mVar[n].

Can someone explain why th开发者_StackOverflow社区is works, if this is good or terrible practice, and what alternative I should use if it is not good practice?


Properties are syntactic sugar for set/get-style methods. Passing arrays in as parameters and out as return values via these methods is fraught with semantic and performance problems, so they probably just put them in the too-hard basket and deliberately excluded them.

As regular data members, arrays don't exhibit these difficulties because you are accessing them directly rather than copying them in and out via methods.

If you want to make the contents of an array accessible as a property (which you should only need to do if you want to make the contents public), you can expose them as:

@property (readonly) int *vars;
@property (readonly) int numVars;

Or you could do the Objective-C thing:

@property (nonatomic, retain) NSArray *vars;

But then you would have to create lots of NSNumber objects (ick).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜