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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-31-2009, 02:31 AM   #1 (permalink)
Noobieee
 
Join Date: Jan 2009
Location: silicon valley, ca
Posts: 16
Default How to add a null array to an NSDictionary

I have a plist file that has an array of dictionaries. Each dictionary item has a string item and an array of strings.

- Array
- Dictionary1
- key1: String
- key2: Array
-String1
-String2
- Dictionary2
- key1: String
- key2: Array
-String1

I tried to add a new dictionary item by executing the following line of code:

NSMutableArray *array2 = nil;
id Dictionary3 = [NSDictionary dictionaryWithObjectsAndKeys: key1, @"key1_string", array2, @"key2"];
[Array insertObjectictionary3 atIndex:[Array count]];

Is this ok or am I completely off the track here?
Should I use NSMutableDictionary instead?
Should I do NSMutableArray *array2 = [NSNull null]; ? but this resulted in Warning when compiled

Then when I try to add a new item to array2, I have the following codes:

NSString *newStr = @"New String Item";
NSMutableDictionary *dict3 = [Array objectAtIndex:[Array count]];
NSMutableArray *key2Array = [dict3 valueForKey:@"key2"];
NSInteger count = [key2Array count];
[array insertObject:newStr atIndex:count];

This failed to add the newStr object to Array-Dict3.

If I added the newStr to Array-Dictionary1 or 2, this worked just fine.

What am I missing here? I'd appreciate any help. Thanks.
krustaeon is offline   Reply With Quote
Old 01-31-2009, 11:33 AM   #2 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,122
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
Old 02-02-2009, 08:55 PM   #3 (permalink)
Noobieee
 
Join Date: Jan 2009
Location: silicon valley, ca
Posts: 16
Default

Whoa... Thank you very much for the detailed explanation. I appreciate it. Hopefully I can help other newbies soon once I learn enough. Thanks again. This part of my code is working now.
krustaeon is offline   Reply With Quote
Reply

Bookmarks

Tags
nsarray, nsdictionary, nsmutablearray, nsmutabledictionary, null

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 260
20 members and 240 guests
ADY, AragornSG, BrianSlick, Dani77, Dattee, dre, glenn_sayers, HDshot, HemiMG, JasonR, karlam963, nobre84, prchn4christ, Rudy, spiderguy84, themathminister, tomtom100, viniciusdamone, vogueestylee, vvenkatachallam
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,884
Threads: 89,229
Posts: 380,763
Top Poster: BrianSlick (7,129)
Welcome to our newest member, karlam963
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:19 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0