I have an init method that takes values from a NSDictionary. This is within my Friend object.
My question is when do I release this particular instantiation of Friend?
- (id)initWithValue:(NSString *)value {
Friend *friend = [[Friend alloc] init];
friend.friendId = [value valueForKeyPath:@"id"];
friend.friendName = [value valueForKeyPath:@"name"];
return friend;
}
I call the init method below to add the Friend objects to a friends array
for (id value in dataDict) {
Friend *friend = [[Friend alloc] initWithValue:value];
[friends addObject:friend];
[friend release];
}
I then do the following in code:
Friend *friend = (Friend *)[friends objectAtIndex:indexPath.row];
If I autorelease the Friend object in the init method then I get a message was sent to deallocated instance when I use the above code to get the particular objects value based on the indexPath.row.
Not directly answering your question, but these lines of code are definitely dangerous:
Instead, you should write like this: