Objective C Naming Convention for an object that it owns

With the latest versions of Xcode that contain static analyzers, some of my objects throw an analyzer error message. In particular, I have an object that owns and is responsible for freeing itself, but also needs to be returned to the caller and possibly saved there manually.

If I have a method such as this + (Foo) newFoo, the analyzer sees the word New and reports a problem to the caller, saying that it is expected to newFooreturn an object with a saving of +1, and it will not be released anywhere. If I call it + (Foo) getFoo, the analyzer reports an error in this method, indicating a potential leak there, because it is not released until it returns.

My class basically looks like this:

+ (Foo *) newFoo {
    Foo *myFoo = [[[Foo new] retain] autorelease];
    [myFoo performSelectorInBackground:@selector(bar) withObject:nil];
    return myFoo;
}

- (void) bar {
    //Do something that might take awhile
    [self release];
}

, , , -. - ?

+3
1

newFoo Foo +1. Cocoa , new . , . , .

Cocoa -ish :

newFoo fooInBackground ( foo ). , . performSelectorInBackground:withObject: , , .

+ (Foo *) fooInBackground {
    Foo *myFoo = [[[Foo alloc] init] autorelease];
    [myFoo performSelectorInBackground:@selector(bar) withObject:nil];
    return myFoo;
}

Sidenote: myFoo fooInBackground, , . , myFoo . , , -, () iPhone, Cocoa.

+2

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


All Articles