Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 01-31-2009, 10:33 AM   #2 (permalink)
RickMaddy
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,121
RickMaddy will become famous soon enough
Default

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];
RickMaddy is offline   Reply With Quote
 

» Advertisements
» Online Users: 446
17 members and 429 guests
apatsufas, brainspoon, CMSLdesign, Dimpleppanchal, dre, elhanche, Evilelement, Gaz, gogoman, iOS.Lover, ketaskin, Kieren Harrold, LegionMD, MAMN84, MysticLine, Robiwan, Sami Gh
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,614
Threads: 94,086
Posts: 402,793
Top Poster: BrianSlick (7,990)
Welcome to our newest member, kurtroom
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:42 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.