我有来自泛型工厂类的以下(简化)代码:
- (id) invokeSetup: (id) object {
// Just an example, subclasses delegate setup to a component that either returns +0 or +1 references
return objc_msgSend(object, @selector(init));
}
- (id) newInstance {
id object = objc_msgSend([NSString class], @selector(alloc));
id replacement = [self invokeSetup: object];
return replacement;
}
分析器在return replacement:
上产生警告:
警告:具有+0保留计数的对象返回给调用方,其中需要+1 (拥有)保留计数
我需要告诉分析器- invokeSetup
返回的引用是+1。
invokeSetup
,因为它是继承的,还有其他的子类,invokeSetup返回+0引用。如果是+1或+0,则只能在运行时检测到。是否有可能在赋值点(id replacement = ...
)以某种方式告诉ARC,引用肯定是+1?
谢谢你,乔晨
发布于 2012-04-12 15:19:36
有关在代码中抑制静态分析器警告的方法,请参见https://stackoverflow.com/a/5833430/1313031
但是的,最好的办法就是重命名newInstance
https://stackoverflow.com/questions/10126167
复制相似问题