1

I have a NSDictionary that contains objects and keys. The Keys hold a Name and number. I would like to insert those objects into a NSMutableArray by using insertObject: atIndex: . Object being the name and the number is the index I would like to place the object in. I now know that NSMutableArrays are able to insert objects at index:6 if there is no 1-5 so how do I make this possible? Any suggestions are very appreciated!

Example Dictionary [dict objectForKey:@"array"]:

 preferences as whole (
        {
        Name = PowerDown;
        btnIndex = 3;
    },
        {
        Name = ClearCache;
        btnIndex = 5;
    },
        {
        Name = KillBGApps;
        btnIndex = 6;
    },
        {
        Name = InfoPanel;
        btnIndex = 2;
    },
        {
        Name = Lock;
        btnIndex = 4;
    },
        {
        Name = Reboot;
        btnIndex = 0;
    },
        {
        Name = Respring;
        btnIndex = 1;
    }
)

What I have so far but crashes when adding objects out of bounds of the array

-(void)loadArray{

 self.buttons = [NSMutableArray array];


    NSMutableArray *tempArray = [[NSMutableArray alloc] init];
    tempArray = [buttonsPrefs objectForKey:@"buttonsToOrder"];


    for(NSDictionary * dict in tempArray)
    {
        if (dict) {

            NSString *btnName = [dict objectForKey:@"Name"];

            NSString *btnIndex = [dict objectForKey:@"btnIndex"];
            NSUInteger index = [btnIndex integerValue];

            NSLog(@"Name = %@",btnName);
            NSLog(@"at index %i",index);

            [self.buttons insertObject: btnName atIndex: index];
        }


    }

}

EDIT: These values "indexes" for the names with change when a user moves the cell

- (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath 
*)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
NSUInteger fromIndex = [fromIndexPath row];
NSUInteger toIndex = [toIndexPath row];
if (fromIndex == toIndex)
    return;
NSMutableDictionary *selectedButton = [[[_buttons objectAtIndex:fromIndex] retain] 
autorelease];
[_buttons removeObjectAtIndex:fromIndex];
[_buttons insertObject:selectedButton atIndex:toIndex];


//[buttonsPrefs setObject:_buttons forKey:@"buttonsToOrder"];
//[buttonsPrefs writeToFile:[self getFilePath] atomically: YES];


}
6
  • 1
    Perhaps you should sort your preferences in ascending order of btnIndex, and then just append the value to your array? Commented Oct 2, 2012 at 3:44
  • That would work for the example given but that array could be in any order such as 3,6,2,1,0,4,5. Once a user moves the cell up and down it will change the value of the integer of that name Commented Oct 2, 2012 at 3:47
  • Yeah Kevin is right, just sort your array on the basis of btnIndex. Is there is specific reason to do so ? Commented Oct 2, 2012 at 3:47
  • You both are right in the sense if it was always going to be the same but if you look at my edited question I think it will be clearer that it is more complexed then just ascending the order. I do appreciate the help Commented Oct 2, 2012 at 4:00
  • 2
    When initialising your array, populate it with [NSNull null] objects and replace them as required. Commented Oct 2, 2012 at 4:00

2 Answers 2

2

Try to fill your target array with some dummy data according to yourDict count like this:

for (int i=0, i<[yourDict count], ++i){
    [yourArray addObject:@"dummyData"];
}

And when you will need to insertObject do this:

for(NSDictionary * dict in tempArray)
{
    if (dict) {

        NSString *btnName = [dict objectForKey:@"Name"];

        NSString *btnIndex = [dict objectForKey:@"btnIndex"];
        NSUInteger index = [btnIndex integerValue];

        [yourArray insertObject:btnName atIndex:index];
        [yourArray removeObjectAtIndex:index+1];
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This would also be a feasible approach! Thanks for your answer. I didn't even think of just using dummy content but this would work. Figure the initWithCapacity: should cover this but obviously not. Thanks agin
0
NSMutableArray insertObject: atIndex:

as per apple docs `

"Note that NSArray objects are not like C arrays. That is, even though you specify a size when you create an array, the specified size is regarded as a “hint”; the actual size of the array is still 0. This means that you cannot insert an object at an index greater than the current count of an array."

Can only fill within valid array value set.Two things you can do.

  1. Sort and fill the array
  2. Fill the array with some default object like a string(cannot be nil) and then replace it.This option is valid if you are filling all the values in array because when used later you have to check weather the value is right there or the default value is in that position

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.