我可以为调试构建以下代码,但不能为xcode中的OS X目标发布:
myclass.h:
@interface myclass : NSObject @property (nonatomic,copy) NSString *name; @endmyclass.m:
@implementation myclass { NSString *_name; } @synthesize name = _name; @end任何想法为什么? 在iOS上,它为发布和调试而构建。
我知道将实例变量声明移到.h会起作用,但我的意图是隐藏实现细节(当然,实际的类比这个例子更复杂)。 在构建64位应用程序时,我已经在Mountain Lion上的最新xcode版本(4.6,build 4H127)上尝试了这一点。
I can build the following code for debug, but not for release for an OS X target in xcode:
myclass.h:
@interface myclass : NSObject @property (nonatomic,copy) NSString *name; @endmyclass.m:
@implementation myclass { NSString *_name; } @synthesize name = _name; @endAny ideas why? On iOS it builds for both release and debug.
I understand that moving the instance variable declaration to the .h will work, but my intention is to hide the implementation details (of course the real class is more complex than this example). I've tried this on the latest xcode version (4.6, build 4H127) on Mountain Lion when building 64-bit apps.
最满意答案
一种方法是使用类扩展。 在你的.m文件中,写
@interface myclass () { NSString *_name; } @end在myclass的@implementation之上。
另一方面,你并不需要声明支持你的属性的实例变量。 如果你只是写
@property (nonatomic, copy) NSString *name;在你的类声明中(代码以@interface myclass : NSObject结尾并以@end结尾),会自动为你生成一个支持NSString * ivar _name ,并且你将能够在myclass的实例方法中访问这个ivar 。 你甚至不需要写@synthesize name = _name; 。 此外,如果您想使用_name以外的变量名称来_name您的属性name ,则不需要声明伊娃; 相反,你可以使用@synthesize
@synthesize name = m_Name;在您的课堂' @implementation块内。
One approach is to use a class extension. In your .m file, write
@interface myclass () { NSString *_name; } @endabove your @implementation for myclass.
On the other hand, you don't actually need to declare the instance variable which is backing your property. If you just write
@property (nonatomic, copy) NSString *name;in your class declaration (the code beginning with @interface myclass : NSObject and ending with @end), a backing NSString * ivar _name will be generated for you automatically, and you'll be able to access this ivar inside the instance methods of myclass. You don't even need to write @synthesize name = _name;. Furthermore, if you want to use a variable name other than _name to back your property name, you needn't declare the ivar; instead you can just use @synthesize
@synthesize name = m_Name;inside your class' @implementation block.
在.m文件中声明Objective C接口的实例变量(处于发布模式)(Declaring instance variables for an Objective C interface in the .m file (in release mode))我可以为调试构建以下代码,但不能为xcode中的OS X目标发布:
myclass.h:
@interface myclass : NSObject @property (nonatomic,copy) NSString *name; @endmyclass.m:
@implementation myclass { NSString *_name; } @synthesize name = _name; @end任何想法为什么? 在iOS上,它为发布和调试而构建。
我知道将实例变量声明移到.h会起作用,但我的意图是隐藏实现细节(当然,实际的类比这个例子更复杂)。 在构建64位应用程序时,我已经在Mountain Lion上的最新xcode版本(4.6,build 4H127)上尝试了这一点。
I can build the following code for debug, but not for release for an OS X target in xcode:
myclass.h:
@interface myclass : NSObject @property (nonatomic,copy) NSString *name; @endmyclass.m:
@implementation myclass { NSString *_name; } @synthesize name = _name; @endAny ideas why? On iOS it builds for both release and debug.
I understand that moving the instance variable declaration to the .h will work, but my intention is to hide the implementation details (of course the real class is more complex than this example). I've tried this on the latest xcode version (4.6, build 4H127) on Mountain Lion when building 64-bit apps.
最满意答案
一种方法是使用类扩展。 在你的.m文件中,写
@interface myclass () { NSString *_name; } @end在myclass的@implementation之上。
另一方面,你并不需要声明支持你的属性的实例变量。 如果你只是写
@property (nonatomic, copy) NSString *name;在你的类声明中(代码以@interface myclass : NSObject结尾并以@end结尾),会自动为你生成一个支持NSString * ivar _name ,并且你将能够在myclass的实例方法中访问这个ivar 。 你甚至不需要写@synthesize name = _name; 。 此外,如果您想使用_name以外的变量名称来_name您的属性name ,则不需要声明伊娃; 相反,你可以使用@synthesize
@synthesize name = m_Name;在您的课堂' @implementation块内。
One approach is to use a class extension. In your .m file, write
@interface myclass () { NSString *_name; } @endabove your @implementation for myclass.
On the other hand, you don't actually need to declare the instance variable which is backing your property. If you just write
@property (nonatomic, copy) NSString *name;in your class declaration (the code beginning with @interface myclass : NSObject and ending with @end), a backing NSString * ivar _name will be generated for you automatically, and you'll be able to access this ivar inside the instance methods of myclass. You don't even need to write @synthesize name = _name;. Furthermore, if you want to use a variable name other than _name to back your property name, you needn't declare the ivar; instead you can just use @synthesize
@synthesize name = m_Name;inside your class' @implementation block.
发布评论