簡體   English   中英

從NSKeyedUnarchiver unarchiveObjectWithData加載時,靜態分析警告未調用init

[英]Static analysis warns of init not called when loading from NSKeyedUnarchiver unarchiveObjectWithData

我收到一個靜態分析錯誤,我不確定是否可以安全地忽略它,或者我是否可以改進設計以在不做太多更改的情況下將其刪除,這就是舊代碼。

這不使用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....]'

    }
}

我的理解是, [NSKeyedUnarchiver unarchiveObjectWithData:objectData]將導致調用"initWithCoder" 這個對象實現NSCoding ,並具有所要求的正確方法NSCoding實現。

這是從靜態分析得出的誤報,還是我可以做得更好?

編譯器發出警告,因為這是一種奇怪的方法。 :)

將“從默認值讀取或創建新的”邏輯移到類方法中。 這樣既可以修復編譯器警告消息,又可以使模式更一致。

+ (instancetype) defaultThingamahoover
{
     ... check defaults database and unarchive ...
     ... or return new ....
}

順便說一句:通常,您不希望將任何大數據推入默認數據庫。 歸檔對象並將其粘貼在其中非常不典型。 默認數據庫通常用於較小的鍵/值對。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM