2012-08-14 101 views
1

我有這個工作使用NSArray,直到我意識到我需要插入一個字符串數組。現在我將其更改爲NSMutableArray,但遇到了我的語法問題。我允許在NSMutable數組中使用componentsJoinedByString嗎?UITextfield問題到NSMutableArray

NSString *item; 
    int numofSeg; 
    int needSeg; 
if (textfield.text == @"::") { 
     numofSeg = 0; 

    } 
    NSMutableArray *dlist = [[NSMutableArray alloc]init]; 
    //take string from textfield and split it into a list on colons. 
    dlist = [textfield.text componentsSeparatedByString:@":"]; 
    //run through list to count how many segments. Non blank ones. 
    for (item in dlist) { 
     if (item != @""){ 
      numofSeg = numofSeg + 1; 
      NSLog(@"%@",numofSeg); 
     } 
    } 
    //determine number of segments 
    needSeg = 8 - numofSeg; 
    while (needSeg > 0) { 
     //insert 0000 at blank spot 
     [dlist insertString:@"0000" atIndex:@""]; 
     needSeg = needSeg - 1; 
    } 
    for (item in dlist) { 
     if (item == @"") { 
      //remove blank spaces from list 
      [dlist removeAllObjects:item]; 
     } 
    } 
    //join the list of times into a string with colon 
    NSString *joinstring = [dlist componentsJoinedByString:@":"]; 
    NSLog(@"%@",joinstring); 

回答

0

我終於明白了。這是我最終用來解決這個問題:

NSString *z = @"0000"; 
z = [z stringByAppendingString:@""]; 
1

我不認爲NSMutableArray有一個名爲insertString:atIndex:方法。您應該改爲使用insertObject:atIndex:

你準確得到了什麼錯誤?

編輯:

如果你只使用NSMutableArray@" "之前插入@"0000",那麼你可以簡單地做:

string = [string stringByReplacingOccurrencesOfString:@" " withString:@" 0000"]; 

或類似的東西。無需使用NSMutableArray

+0

我的主要問題是試圖在NSMutableArray中使用componentsJoinedByString。我現在有它的工作,但將其轉換回NSArray。我需要基本上在字符串@「」的索引處插入一個字符串。不在索引:0。這是我用於NSArray * item = [ipv6display.text componentsSeparatedByString:@「:」]; – Taken 2012-08-15 12:56:31

+0

我基本上需要弄清楚如何獲取MutableArray dlist並在空白處插入sttring。 [dlist insertString:@「0000」atString:@「」] ;.在Python中,我可以這樣寫:list1.insert(list1.index(''),'0000') – Taken 2012-08-15 13:06:38

+0

你是什麼意思「在字符串索引@」「」?你能給我一個你想要做什麼的例子嗎?你究竟遇到了什麼問題? – aforaudrey 2012-08-15 15:55:17