1

我目前正在使用 xcode 进行一些 c++ 开发,我需要生成 getter 和 setter。

我知道的唯一方法是以 Objective C 风格生成 getter 和 setter

像这样的东西 - (string)name; - (void)setName:(string)value;

我不想要这个;我想要在头文件中使用带有实现和声明的 c++ 样式生成。

任何的想法...?

4

3 回答 3

6

听起来您一直在寻找一种方法来减少编写 getter/setter(即属性/综合语句)的麻烦,对吗?

您可以在 XCode 中使用一个免费的,甚至可以在突出显示一个我觉得非常有用的成员变量后自动生成 @property 和 @synthesize 语句 :)

如果您正在寻找更强大的工具,您可能需要查看另一个名为Accessorizer的付费工具。

于 2009-10-28T23:58:42.550 回答
3

目标 C != C++。

ObjectiveC 使用 @property 和 @synthesize 关键字为您提供自动实现(我自己目前正在学习 ObjectiveC,刚买了一台 Mac!)。C++ 没有这样的东西,所以你只需要自己编写函数。

Foo.h

inline int GetBar( ) { return b; }
inline void SetBar( int b ) { _b = b; }    

或者

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; }
于 2009-10-28T23:43:39.353 回答
-1

某事.h:

@interface something : NSObject
{
   NSString *_sName;  //local
}

@property (nonatomic, retain) NSString *sName;

@end

某事.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
于 2009-10-28T23:50:59.210 回答