1

我使用 NSArray 进行了这项工作,直到我意识到我需要将一个字符串插入到数组中。现在我将其更改为 NSMutableArray,但我的语法有问题。我可以在 NSMutable 数组中使用 componentsJoinedByString 吗?

    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);
4

2 回答 2

1

我不认为NSMutableArray有一个方法叫做insertString:atIndex:. 您可能应该insertObject:atIndex:改用。

你到底得到了什么错误?

编辑:

如果您只使用NSMutableArrayto insert @"0000"before @" ",那么您可以简单地执行以下操作:

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

或类似的东西。无需使用NSMutableArray.

于 2012-08-14T22:19:55.983 回答
0

我终于弄明白了。这就是我最终用来解决这个问题的方法:

NSString *z = @"0000";
z =  [z stringByAppendingString:@""]; 
于 2012-08-16T16:44:43.027 回答