I am new to ARC, and I have an object which has some internal classes as members. On the init method I want to allocate new objects for them.
ClassA.h
#import "ClassB.h"
@interface ClassA : NSObject
@property (assign) ClassB *member;
@end
ClassB.h
@interface ClassB : NSObject
@property (assign) NSString *name;
@end
ClassA.m
@synthesize member = _member;
-(id)init
{
_member = [[ClassB alloc] init];
}
But I get "Assigning retained object to unsafe property" errors. I searched over the inter webs, and see no other information on this specific warning. It compiles, but gets a runtime bad access exception.
The immediate problem is that you're assigning the object to a member marked weak
, which means that the object won't have a strong reference and will be deallocated immediately. Using strong
or retain
instead of weak
or assign
will fix that.
A larger problem with your -init
method is that it doesn't call [super init]
, and it doesn't return anything. At minimum, your -init
should look like this:
-(id)init
{
self = [super init];
if (self != nil) {
self.member = [[ClassB alloc] init];
}
return self;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With