本文介绍了从[[class alloc] init]返回nil是否被视为良好实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是一个常见的成语在Objective-C。
And is it a common idiom in Objective-C.
我只看到这个使用[[NSImage alloc] initWithContentsOfFile:str]我认为有一个内存泄漏,因为我调用alloc和咒语是:
调用alloc,你必须调用release - 除非你不需要的情况下。
I've only seen this used on [[NSImage alloc] initWithContentsOfFile: str] and it always make me think there is a memory leak, because i called alloc and the mantra is:"Call alloc and you must call release" - unless its one of the cases where you don't need to.
推荐答案
这是一个常见的成语,表示初始化对象时出现错误。你是正确的,但是,分配的实例必须被释放。所以模式是
It is a common idiom to indicate a error in initializing the object. You are correct, however, the allocated instance must be released. So the pattern would be
- (id)init
{
self = [super init];
if(self != nil) {
//... do init
if(errorInInit) {
[self release];
return nil;
}
}
return self;
}
这篇关于从[[class alloc] init]返回nil是否被视为良好实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!