开发者

why is ivars needed in Cocoa when they aren't needed in iOS?

开发者 https://www.devze.com 2023-03-10 00:40 出处:网络
I kind of like the iOS way of开发者_如何学运维 not having to specify ivars, but I dunno wether it\'s possible to do the same in Cocoa or if it\'s a bad idea.

I kind of like the iOS way of开发者_如何学运维 not having to specify ivars, but I dunno wether it's possible to do the same in Cocoa or if it's a bad idea.


Here is the same code in Cocoa and iOS

// Cocoa
@interface Foobar : NSObject {
    NSString* m_name;
}
@property (nonatomic, retain) NSString* name;
@end

@implementation Foobar
@synthesize name = m_name;
@end

// iOS
@interface Foobar : NSObject {
    // no ivar here
}
@property (nonatomic, retain) NSString* name;
@end

@implementation Foobar
@synthesize name;
@end


The ability to elide ivars requires the “non-fragile” Objective-C runtime. 32-bit versions of Mac OS X use the “fragile” runtime. If you’re targeting 64-bit systems only, you can do it the same way is on iOS.

0

精彩评论

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