1
votes

I have some doubts in objective-C programming. I have function like this.

+ (NSManagedObjectContext *) newContext {
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil)
{
    NSManagedObjectContext* managedObjectContext = [[[NSManagedObjectContext alloc] init] autorelease];
    [managedObjectContext setPersistentStoreCoordinator:coordinator];
    return managedObjectContext;
}
return nil;

}

But when I run analyse on my project I am seeing something like this with warning

"Object with a +0 retain count returned to caller where a +1 (owning) retain count is expected"

What I see after I run analyse

Can someone point me out so as to why Xcode is giving me a warning here. What is the correct way to return any variable?

1

1 Answers

4
votes

The problem lies not within your code but within the name of your method. In Objective C, methods whose names start with init or new are assumed by the analyser to return an object with a retain count of 1. You are returning an object with a retain count of zero, therefore your method name should not contain new.