Why can't I highlight / init - get the warning "Assigning a saved object to an unsafe property"?

I am new to ARC and I have an object that has some inner classes as members. In 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 errors "Assigning a saved object to unsafe properties." I was browsing websites and did not see any other information about this particular warning. It compiles, but receives an exception for safe execution at runtime.

+7
source share
1 answer

The immediate problem is that you assign the object to the member marked weak , which means that the object will not have a strong reference and will be immediately released. Using strong or retain instead of weak or assign fix this.

The big problem with your -init method is that it does not call [super init] and it does not return anything. At a minimum, your -init should look like this:

 -(id)init { self = [super init]; if (self != nil) { self.member = [[ClassB alloc] init]; } return self; } 
+12
source

All Articles