[英]Static analysis warns of init not called when loading from NSKeyedUnarchiver unarchiveObjectWithData
I'm receiving a static analysis error, and I'm not sure if it can be safely ignored, or if I can improve the design to remove it without to much change, this is legacy code. 我收到一个静态分析错误,我不确定是否可以安全地忽略它,或者我是否可以改进设计以在不做太多更改的情况下将其删除,这就是旧代码。
This does NOT use ARC. 这不使用ARC。
-(id) initCustom{
NSString* key = @"foo";
NSData* objectData = nil;
objectData = [[NSUserDefaults standardUserDefaults] objectForKey:key];
if( objectData != nil)
{
//If this path is taken the error occurs
self = [NSKeyedUnarchiver unarchiveObjectWithData:objectData];
}
else
{
self = [super init];
}
if (self)
{
//Static analysis warns here
m_fiz = [[NSString alloc] initWithString:@"bar"];
//Instance variable used while 'self' is not set to the result of '[(super or self) init....]'
}
}
My understanding is that [NSKeyedUnarchiver unarchiveObjectWithData:objectData]
will cause the "initWithCoder"
to be called. 我的理解是,
[NSKeyedUnarchiver unarchiveObjectWithData:objectData]
将导致调用"initWithCoder"
。 This object implements NSCoding
, and has the proper methods required by NSCoding
implemented. 这个对象实现
NSCoding
,并具有所要求的正确方法NSCoding
实现。
Is this a false positive from the static analysis, or can I make it better? 这是从静态分析得出的误报,还是我可以做得更好?
The compiler is warning because that is a bizarre way of doing this. 编译器发出警告,因为这是一种奇怪的方法。 :)
:)
Move the "read from defaults or create new" logic into a class method. 将“从默认值读取或创建新的”逻辑移到类方法中。 That will both fix the compiler warning message and be a more consistent pattern.
这样既可以修复编译器警告消息,又可以使模式更一致。
+ (instancetype) defaultThingamahoover
{
... check defaults database and unarchive ...
... or return new ....
}
BTW: In general, you don't want to shove anything large into the defaults database. 顺便说一句:通常,您不希望将任何大数据推入默认数据库。 It is pretty atypical to archive an object and stick it in there.
归档对象并将其粘贴在其中非常不典型。 The defaults database is generally intended for small key/value pairs.
默认数据库通常用于较小的键/值对。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.