I am working through a book on Cocoa and Objective-C. One example has stumped me:
- (id) currentObject {
return [Photo photo];
}
- (void) checkObjectType {
id object = [self currentObject];
...
}
In this case, checkObjectType calls currentObject. currentObject then returns an instance of Photo, however, it’s creating a new instance of Photo and returning this reference, is it not ? Isn’t it not return a reference to itself ? I was expecting something like:
return self;
Thanks,
Scott
You must be referring to Scott Stevenson’s book. The example given is just trying to show how to use the
isMemberOfClass:method. I wouldn’t read any more into it than that.Your confusion is understandable. As you’ve already figured out, the
currentObject:method returns a newly-created autoreleased object, not the object itself as its name would imply. It’s a poorly named method. Maybe it’s just a sneaky way of reminding the reader to name methods appropriately. 🙂