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

我所知道的唯一方法就是产生吸气剂和吸气剂。 Objective C风格的setter

像这样的事情 - (字符串)名称; - (void)setName:(string)value;

我不想要这个;我希望c ++风格生成与实现&在头文件中使用的声明。

任何想法......?

有帮助吗?

解决方案

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

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

如果您正在寻找更强大的工具,还有另一种名为 Accessorizer 你可能想看看。

其他提示

目标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; }

something.h:

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

@property (nonatomic, retain) NSString *sName;

@end

something.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
scroll top