开发者

How do I make a custom unretained class?

开发者 https://www.devze.com 2023-03-24 12:56 出处:网络
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 开发者_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];
0

精彩评论

暂无评论...
验证码 换一张
取 消