Taken
Taken

Reputation: 27

Issue with UITextfield into a NSMutableArray

I had this working using NSArray until I realize I need to insert a string into array. Now I'm changing this to a NSMutableArray, but having issues with my syntax. Am I allowed to use componentsJoinedByString in NSMutable arrays?

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

Upvotes: 1

Views: 106

Answers (2)

Taken
Taken

Reputation: 27

I finally figured it out. This is what I ended up using to fix this:

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

Upvotes: 0

danqing
danqing

Reputation: 3668

I don't think NSMutableArray has a method called insertString:atIndex:. You should probably use insertObject:atIndex: instead.

What error do you get exactly?

Edit:

If you only use the NSMutableArray to insert @"0000" before @" ", then you can simply do:

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

or something like that. No need to use NSMutableArray.

Upvotes: 1

Related Questions