You posted this code:
Code:
NSMutableArray *array2 = nil;
id Dictionary3 = [NSDictionary dictionaryWithObjectsAndKeys: key1, @"key1_string", array2, @"key2"];
[Array insertObjectictionary3 atIndex:[Array count]];
Guideline 1:
Class names use CamelCase and start with uppercase letters.
Variable names use camelCase and start with lowercase letters.
Underscores can be used in place of spaces and camelcase.
Guideline 2:
Don't use 'id' for variables unless there is a very specific reason to do so.
Guideline 3:
Name your variables something helpful. 'Array' is a terrible variable name. You already know it's an array. Give a good name that represents what is actually in the array, in layman's terms.
Problem 1:
You don't have the required 'nil' as the last argument to 'dictionaryWithObjectsAndKeys:'.
Suggestion:
Use 'addObject:' when adding an element to the end of the array instead of 'insertObject:AtIndex:'
Suggestion:
When posting code here, format it with '[ CODE]...[ /CODE]' (but without the space)
So your code should be:
Code:
NSMutableArray *array2 = nil;
NSDictionary *dictionary3 = [NSDictionary dictionaryWithObjectsAndKeys: key1, @"key1_string", array2, @"key2", nil];
[array addObject:dictionary3];
But of course this will fail because you can have a 'nil' key or value in a dictionary. So you tried this:
Code:
NSMutableArray *array2 = [NSNull null];
This didn't work because you can't assign an 'NSNull' object to an 'NSArray'. So change the original code to:
Code:
NSDictionary *dictionary3 = [NSDictionary dictionaryWithObjectsAndKeys: key1, @"key1_string", [NSNull null], @"key2", nil];
[array addObject:dictionary3];
Now your next block of code:
Code:
NSString *newStr = @"New String Item";
// This won't work because you added an NSDictionary, not NSMutableDictionary
// This also will blow up because the last index of an array is 'count - 1', not 'count'
NSMutableDictionary *dict3 = [Array objectAtIndex:[Array count]];
// This won't work because you added NSNull, not NSMutableArray
NSMutableArray *key2Array = [dict3 valueForKey:@"key2"];
// This will blow up because 'NSNull' doesn't have a 'count' method
NSInteger count = [key2Array count];
// What is 'array'?
[array insertObject:newStr atIndex:count];
Since it appears you want to add things to the original 'array2', why add a 'nil' or 'NSNull' array? Just add an empty array. Since you need to modify 'dictionary3' then add a mutable dictionary. So now the original code gets modified once again to:
Code:
NSMutableArray *array2 = [NSMutableArray array];
NSMutableDictionary dictionary3 = [NSMutableDictionary dictionaryWithObjectsAndKeys: key1, @"key1_string", array2, @"key2", nil];
[array addObject:dictionary3];
And now the second block becomes:
Code:
NSMutableDictionary *dict3 = [array objectAtIndex:[array count] - 1];
NSMutableArray *key2Array = [dict3 valueForKey:@"key2"];
NSInteger count = [key2Array count];
// Move 'newStr' to where you actually need it.
NSString *newStr = @"New String Item";
// I left this as-is because I don't know what it is
[array insertObject:newStr atIndex:count];