我可能忽略了一些小东西,但我似乎无法弄清楚。
我正在尝试将一个自定义类的实例传递给另一个自定义类的实例。 注意:我正在使用 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 时,仍然出现错误。我错过了什么?
最佳答案
该属性应声明为 strong。下面是我如何编写相同的代码:
#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
关于iphone - 将 ivar 设置为自定义子分类对象不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10342349/