在 XCode 中生成 getter 和 setter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1640848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generating getters & setters in XCode
提问by Asad Khan
I am currently using xcode for some c++ development & I need to generate getters & setters.
我目前正在使用 xcode 进行一些 C++ 开发,我需要生成 getter 和 setter。
The only way I know is generating getters & setters in Objective C style
我知道的唯一方法是以 Objective C 风格生成 getter 和 setter
something like this - (string)name; - (void)setName:(string)value;
像这样 - (string)name; - (void)setName:(string)value;
I dont want this; I want c++ style generation with implementation & declaration for use in the header files.
我不要这个;我想要在头文件中使用带有实现和声明的 C++ 样式生成。
Any idea...?
任何的想法...?
回答by Ray Wenderlich
It sounds like you're just looking for a way to reduce the hassle of writing getters/setters (i.e. property/synthesize statements) all the time right?
听起来您只是在寻找一种方法来减少一直编写 getter/setter(即属性/合成语句)的麻烦,对吗?
There's a free macroyou can use in XCode to even generate the @property and @synthesize statements automatically after highlighting a member variable that I find really helpful :)
有一个免费的宏,您可以在 XCode 中使用它甚至在突出显示我觉得非常有用的成员变量后自动生成 @property 和 @synthesize 语句:)
If you're looking for a more robust tool, there's another paid tool called Accessorizerthat you might want to check out.
如果您正在寻找更强大的工具,您可能需要查看另一个名为Accessorizer的付费工具。
回答by Ed S.
Objective C != C++.
目标 C != C++。
ObjectiveC gives you auto-implementation using the @property and @synthesize keywords (I am currently leaning ObjectiveC myself, just got a Mac!). C++ has nothing like that, so you simply need to write the functions yourself.
ObjectiveC 为您提供使用@property 和@synthesize 关键字的自动实现(我目前自己也在学习 ObjectiveC,刚买了一台 Mac!)。C++ 没有这样的东西,所以你只需要自己编写函数。
Foo.h
foo.h
inline int GetBar( ) { return b; }
inline void SetBar( int b ) { _b = b; }
or
或者
Foo.h
foo.h
int GetBar( );
void SetBar( int b );
Foo.cpp
文件
#include "Foo.h"
int Foo::GetBar( ) { return _b; }
void Foo::SetBar( int b ) { _b = b; }
回答by inked
something.h:
东西.h:
@interface something : NSObject
{
NSString *_sName; //local
}
@property (nonatomic, retain) NSString *sName;
@end
something.m :
东西.m :
#import "something.h"
@implementation something
@synthesize sName=_sName; //this does the set/get
-(id)init
{
...
self.sName = [[NSString alloc] init];
...
}
...
-(void)dealloc
{
[self.sName release];
}
@end