質問

I'm trying to understand this from a memory management point of view: In one class, I'm making a helper method that will create an NSDictionary object for me.

+(NSDictionary*) getTheDictionary{
    return [[[NSDictionary alloc] initWithObjectsAndKeys:
        @"value", @"key", nil] autorelease];
}

From another class, I use the method.

NSDictionary* theDictionary = [HelperClass getTheDictionary];

Is it enough to just have "autorelease" in the return statement? Do I also need an autorelease on theDictionary?

役に立ちましたか?

解決

getTheDictionary returns an autoreleased object, which means that the object is valid in the calling method, but not owned by the caller. Therefore the calling method must not release or autorelease that object.

It will be released when the current autorelease pool ends, e.g. when program control returns to the main event loop.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top