1
は次のとおりです。なぜ他のクラスから関連オブジェクトを取得できないのですか?</p> <p>に別のクラスから私のコードを関連付けられているオブジェクトを取得する方法
#import <UIKit/UIKit.h>
static char NUMBER ='a';
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
@implementation Person
- (instancetype)init
{
self = [super init];
if (self) {
NSNumber *[email protected];
objc_setAssociatedObject(self, &NUMBER, num, OBJC_ASSOCIATION_RETAIN);
}
return self;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
Person *p=[[Person alloc]init];
NSNumber *num=objc_getAssociatedObject(p, &NUMBER);
NSLog(@"%@",num);
}
@end
NSLog(@"%@",num)
がnullです。
なぜ上記のコードから関連オブジェクトを取得できないのですか?他のクラスから関連オブジェクトを取得できないのですか?ありがとうございました!