Coding example?


#1

Hey guys,

I LOVE this book. I’m an Apple fan but new to Objective-C. Hands down the best programming ever.

I’m working through chapter three and learning about the difference between strong and weak properties. Towards the bottom of page 82 we have this:

@interface Person : NSObject
@property (nonatomic, strong) Person *spouse;
@propety (nonatomic, copy) NSString *lastName;
@property (nonatomic, copy) NSString *lastNameOfSpouse;
@end

This is a minor issue, but shouldn’t the spouse property be weak instead of strong? I’ve tested this little class in Xcode. Here’s what the code in my main looks like:

Person *person1 = [[Person alloc] init];
Person *person2 = [[Person alloc] init];
        
person1.spouse = person2;
person2.spouse = person1;

person1 = nil;
person2 = nil;

When the spouse property is strong, setting the person pointers to nil doesn’t deallocate the objects. When the spouse property is weak, it does deallocate them.

Is there a reason it’s set to strong instead of weak here? Wondering what I’m missing. Seems like as it stands, we are risking a memory leak.

thanks,
Martyn