Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa 为什么可可中需要IVAR,而不是';iOS不需要吗?_Cocoa_Ios_Properties - Fatal编程技术网

Cocoa 为什么可可中需要IVAR,而不是';iOS不需要吗?

Cocoa 为什么可可中需要IVAR,而不是';iOS不需要吗?,cocoa,ios,properties,Cocoa,Ios,Properties,我有点喜欢iOS不必指定IVAR的方式,但我不知道在Cocoa中是否可以这样做,或者这是一个坏主意 下面是Cocoa和iOS中的相同代码 // Cocoa @interface Foobar : NSObject { NSString* m_name; } @property (nonatomic, retain) NSString* name; @end @implementation Foobar @synthesize name = m_name; @end 消除IVAR的

我有点喜欢iOS不必指定IVAR的方式,但我不知道在Cocoa中是否可以这样做,或者这是一个坏主意


下面是Cocoa和iOS中的相同代码

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

@implementation Foobar
@synthesize name = m_name;
@end


消除IVAR的能力需要“非脆弱”的Objective-C运行时。32位版本的Mac OS X使用“脆弱”运行时。如果你只针对64位系统,你可以像在iOS上那样做。

我一直在iOS代码中指定IVAR。我也这样做过,但我的iOS朋友指出这是没有必要的。这让我很困惑。您还可以完全从
@接口中省略实例变量部分(
{…}
),当您将其包含在
@implementation
中时,您可以将其包含在
@实现中,而不是
@interface
。自从我发现这一点以来,我的编码风格就是只将instance variables部分放在
@implementation
中,如果有的话。啊哈,在阅读了“易碎”之后,我终于明白了。非常感谢。
// iOS
@interface Foobar : NSObject {
    // no ivar here
}
@property (nonatomic, retain) NSString* name;
@end

@implementation Foobar
@synthesize name;
@end