开发者

Why Objective-C properties are sort of inconvenient?

It is more of a complain than a question, though maybe someone has some good points on i开发者_运维问答t. So basically if you want an ivar in your Objective-C class have accessor-methods you have to mention it 3 times

SomeClass* _ivar;
@property (nonatomic,retain/assign/copy) SomeClass* ivar;
@synthesize ivar = _ivar;

and maybe 4th time in dealloc method. So wouldn't it be more convenient if the approach would be like Java-style annotations - in one place before the actual ivar declaration, just something like:

@property (nonatomic,retain,synthesize = ivar,dealloc) SomeClass* _ivar; 

this also generates accessor methods, and dealloc - telling to dealloc the ivar in dealloc method.


Actually you don't have to declare ivar - they can be synthesized if you just declare property for them. This should synthesize name iVar for you: (not supported in legacy run-times though - so one of the reasons of this seemingly redundant syntax is for backward compatibility with legacy platforms)

@interface MyClass : NSObject 
{
}

@property(copy) NSString *name;

@end

...
@synthesize name;

In new XCode version (4.0 probably) you won't need to use @synthesize as well - properties will be synthesized by default.

So as you see objective-c develops to satisfy your wishes :)


Xcode 4 doesn't automatically synthesize properties unfortunately. But with ARC (automatic reference counting) you no longer need to worry about dealloc and instance variables anymore.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜