简体   繁体   English

从NSKeyedUnarchiver unarchiveObjectWithData加载时,静态分析警告未调用init

[英]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.

相关问题 Swift-NSMutableArray中的NSKeyedUnarchiver unarchiveObjectWithData - Swift - NSKeyedUnarchiver unarchiveObjectWithData in NSMutableArray NSKeyedUnarchiver unarchiveObjectWithData:不起作用 - NSKeyedUnarchiver unarchiveObjectWithData: not working NSKeyedUnarchiver unarchiveObjectWithData返回nil - NSKeyedUnarchiver unarchiveObjectWithData returning nil 不从unarchiveObjectWithData调用initWithCoder:函数: - The initWithCoder: function is not called from unarchiveObjectWithData: NSKeyedUnarchiver unarchiveObjectWithData:解析块函数中永不返回 - NSKeyedUnarchiver unarchiveObjectWithData: in Parse block function never returns 如何将 NSKeyedUnarchiver unarchiveObjectWithData 更新为 NSKeyedUnarchiver unarchivedObjectOfClass:[fromData:error: - How to update NSKeyedUnarchiver unarchiveObjectWithData to NSKeyedUnarchiver unarchivedObjectOfClass:[fromData:error: 加载从其他目标创建的类时,NSKeyedUnarchiver.unarchiveObjectWithFile崩溃 - NSKeyedUnarchiver.unarchiveObjectWithFile crashes when loading classes created from a different target NSKeyedUnarchiver。 从plist加载自定义对象数组 - NSKeyedUnarchiver. Loading array of custom objects from plist 在初始化过程中使用NSKeyedUnarchiver遇到问题 - Trouble Using NSKeyedUnarchiver during init 从app delegate调用时,调用工具包ui未加载 - call kit ui not loading when called from app delegate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM