2011-04-28 71 views
0

我想解析我輸入字符串所需的數據,但是我不斷收到錯誤。有人可能會讀這段代碼,並告訴我我錯了。NSarray錯誤,當設置uibutton

NSArray *AllDataArray = [RawMessage componentsSeparatedByString:@"|"]; 

    NSLog(@"%@", AllDataArray); 
    //  Should we bomb Libya ?,0,0,06/04/2011,30/04/2011|Will England win in the olympics ?,0,0,18/04/2011,18/05/2011|Is White the new Black ?,0,0,21/04/2011,21/05/2011| 

    //do a for loop and and the questions were ever needed using [AllDataArray objectAtIndex:0]; 
    NSArray *Question1 = [[AllDataArray objectAtIndex:0] componentsSeparatedByString:@","]; 
    NSArray *Question2 = [[AllDataArray objectAtIndex:1] componentsSeparatedByString:@","]; 
    NSArray *Question3 = [[AllDataArray objectAtIndex:2] componentsSeparatedByString:@","]; 

    //Update ui buttons 
    [btnQuestion1 setTitle:[Question1 objectAtIndex:0] forState:UIControlStateNormal]; 
    [btnQuestion1 setTitle:[Question1 objectAtIndex:0] forState:UIControlStateHighlighted]; 
    [btnQuestion1 setTitle:[Question1 objectAtIndex:0] forState:UIControlStateDisabled]; 
    [btnQuestion1 setTitle:[Question1 objectAtIndex:0] forState:UIControlStateSelected]; 

錯誤我得到的是在man.m SIGABRT的時候我都沒有碰過的main.m

#import <UIKit/UIKit.h> 

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

什麼是錯誤信息?並通過'(@「%@」,AllDataArray)丟失了NSLog;' – Joe 2011-04-28 13:19:45

+0

你能發佈堆棧跟蹤嗎?它可能表明什麼導致了sigabrt。 – dredful 2011-04-28 13:39:52

回答

0

錯誤可能不是你剛纔發佈的代碼,因爲我只是把你的代碼,重新創建了一個項目,它運行良好,沒有錯誤。

但是..有一件事情可能在其他地方引起問題是你的AllDataArray。在解析字符串「|」後,查看輸出結果,最後一個項目中會出現一個空字符串...並且,我假設您稍後根據「,」分割該字符串..和我可能會認爲您引用數組的順序位置沒有做任何邊界檢查,就像你在這裏做

NSArray *Question1 = [[AllDataArray objectAtIndex:0] componentsSeparatedByString:@","]; 

如果數組沒有任何元素,就拋出一個錯誤,如果未處理應該冒泡到main();

但是..再次,那裏有很多假設。