1

何か小さなものを見落としているのかもしれませんが、理解できないようです。

カスタムクラスのインスタンスを別のカスタムクラスのインスタンスに渡そうとしています。 注:私はARC*を使用しています

2番目のカスタムクラスが設定されます。

#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

プロパティは 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
于 2012-04-26T22:51:57.563 に答える