1

我可能忽略了一些小东西,但我似乎无法弄清楚。

我正在尝试将自定义类的实例传递给另一个自定义类的实例。 注意:我正在使用 ARC*

设置了第二个自定义类:

#import "OneArtsDay.h"

@interface SocialButton : UIButton {    
    OneArtsDay *artsDay;
}

@property (nonatomic) OneArtsDay *artsDay;

- (void)setArtsDay:(OneArtsDay *)day;

@end

#import "SocialButton.h"

@implementation SocialButton
@synthesize artsDay;

- (void)setArtsDay:(OneArtsDay *)day {
  if (day ==nil) {
    NSLog(@"Error, cannot set artsDay");
  }
  else {
  artsDay = day;
  }
}

@end

现在,当我在代码中调用这些命令时:

    SocialButton *social = [[SocialButton alloc] init];
    OneArtsDay *day = [[OneArtsDay alloc] init];
    //Do things with day here//
    [social setArtsDay:day];

当我尝试访问属性 OneArtsDay *artsDay 时,仍然出现错误。我错过了什么?

4

1 回答 1

2

该属性应声明为强。这是我编写相同代码的方式:

#import "OneArtsDay.h"

@interface SocialButton : UIButton

// property decl gives me the file var and the public getter/setter decls
// strong tells ARC to retain the value upon assignment (and release the old one)
@property (nonatomic, strong) OneArtsDay *artsDay;

@end


#import "SocialButton.h"

@implementation SocialButton

// _underscore alias let's me name stack vars and prams the same name as my property
// without ambiguity/compiler warnings

@synthesize artsDay=_artsDay;

- (void)setArtsDay:(OneArtsDay *)artsDay {
    if (artsDay==nil) {
        NSLog(@"Error, cannot set artsDay");
    } else {
        _artsDay = artsDay;
    }
}

@end
于 2012-04-26T22:51:57.563 回答