How 开发者_JS百科can I make it so that I can get an unretained instance of a class? You can do this with various Cocoa classes like NSString
([NSString string]
) or NSArray
([NSArray array]
).
How can I do this with my custom class so I can call [MyClass class]
instead of [[MyClass alloc]init]
?
Implement class method in your class that returns autoreleased object:
// Header
+(MyClass*) myClass;
// implementation
+(MyClass*) myClass{
return [[[MyClass alloc] init] autorelease];
}
Then in your code you'll be able to get your 'unretained' aka autoreleased instance of your custom class:
MyClass *myObj = [MyClass myClass];
精彩评论