为什么我的@property没有自动吸气器?

时间:2013-06-30 18:11:09

标签: objective-c

我已经宣布了一个名为Squad的属性,但是当我发送[self getSquad]时,我得到了#34; SquadViewController没有可见的@interface声明了选择器' getSquad'"。

SquadViewController.h:

#import "FlipsideViewController.h"
#import "Squad.h"

@interface SquadViewController : UIViewController <FlipsideViewControllerDelegate, UIPopoverControllerDelegate>

@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;

@property (strong, nonatomic) UIPopoverController *flipsidePopoverController;

@property (nonatomic, retain) IBOutlet UILabel *squadNameLabel;

@property Squad *squad;

- (IBAction)updateTitleWithName:(id)sender;

@end

SquadViewController.m:

#import "SquadViewController.h"

@interface SquadViewController ()

@end

@implementation SquadViewController

@synthesize squadNameLabel;
@synthesize squad;

...

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    if (![self getSquad]) //<--THIS IS WHERE THE ERROR IS
    {
        [self setSquad:[Squad squadWithName:@"New Squad"]]; //<-- NOT HERE, SO THE SETTER SEEMS TO EXIST
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

...

我认为&#34; @synthesize小队&#34;会生成getSquad和setSquad,所以我很困惑。

这里是Squad的代码,如果由于某些原因我没有得到它的相关性(我是Objective C的新手,我仍然觉得它很混乱(我和#39;来自java背景)):

Squad.h:

#import "SquadBuilderObject.h"

@interface Squad : NSObject

@property NSString *name;

+ (id) squadWithName:(NSString*)name;

@end

Squad.m:

#import "Squad.h"

@implementation Squad

@synthesize name;

+ (id)squadWithName:(NSString *)name
{
    Squad *newSquad = [[Squad alloc] init];
    [newSquad setName:name];
    return newSquad;
}

@end

1 个答案:

答案 0 :(得分:8)

名为squad的属性的标准getter为squad,而不是getSquad

“get ...”命名法通常保留用于引用返回的内容(例如- (BOOL)getSquad:(Squad **)outSquad)。