2011-06-09 63 views
5
- (void)main { 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // Warning goes here 

    NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; 
    while (YES) { 
     NSAutoreleasePool *subPool = [[NSAutoreleasePool alloc] init]; 
     [runLoop run]; 
     [subPool drain]; 
    } 

    [pool drain]; 
} 

我不明白爲什麼這個代碼得到這樣的警告,尤其是當它幾乎完全一樣的結構,在main.m文件主要功能由Xcode本身產生,它沒有得到相同的警告:鏘警告:它的初始化過程中存儲到「池」值永遠不會讀

int main(int argc, char *argv[]) 
{ 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
    int retVal = UIApplicationMain(argc, argv, nil, nil); 
    [pool release]; 
    return retVal; 
} 

回答

5

我相信問題是while (YES)聲明。 Clang認爲這是永無止境的永無止境的循環,因此該塊以下的代碼將永遠無法到達。

只是將其更改爲以下(a BOOL變量設置爲YES外塊)將刪除警告:

int main (int argc, const char * argv[]) 
{ 

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

    NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; 

    BOOL keepRunning = YES; 

    while (keepRunning) { 
     NSAutoreleasePool *subPool = [[NSAutoreleasePool alloc] init]; 
     [runLoop run]; 
     [subPool drain]; 
    } 

    [pool drain]; 

    return 0; 
} 
+0

很好的答案 – 2015-12-31 12:23:51

相關問題