Class method for returning an object with auto-implementation

I have been watching iPhone development videos on iTunes U, and am still so good. I think I understood things well enough.

The point is, in the examples they provide, they never create their own class methods, similar to those you use on some Foundation classes (for example, [NSString string]), so I'm not sure how I should create my own class method to return an instance with auto-implementation of my class.

I know how to create a saved object using an instance method, but I would prefer to use a class method because I prefer it, I'm just not sure if this implementation will be most suitable for returning an object with auto-implementation:

+ (PhotoViewController*)initWithImageView:(UIImageView*)imageView
{
    PhotoViewController *toreturn = [[PhotoViewController alloc] init];
    toreturn.imageview = imageView;
    [toreturn autorelease];
    return toreturn;
}

Thanks so much for any help you can provide.

+3
source share
2 answers

The class method can return either a stored or an auto-implemented object as you wish, and your code automatically returns an object with auto-implementation.

However, you should probably name your method differently. Since your method starts with init, this means that it initializes the alloced object (and therefore should be an instance method, not a class method). I would suggest calling a method photoViewControllerWithImageView:if it returns an auto-implemented object.

Also, I would probably write it as return [toreturn autorelease];, but I assume that preference is in my style.

+6
source

, toreturn nil imageview.

+2

Source: https://habr.com/ru/post/1790747/


All Articles