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 02-15-2010, 06:57 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 83
Default Storing custom structs in Dictionary

I remember watching a video on iTunes U, I cant find it again but someone mentioned that you can't store any kind of object in an NSArray or NSDictionary.

I ran into some trouble trying to save a struct into a dictionary, to make things more complex, the struct had a NSDictionary as one of it's properties.

The error message I get is: incompatible type for argument 1 of 'setObject:forKey:'

Am I correct when I say you can't store a struct in a NSDictionary ?

What workaround would you suggest if that's the case ?
djp_phillips is offline   Reply With Quote
Old 02-15-2010, 07:13 AM   #2 (permalink)
Obj-C Learner
 
Join Date: Apr 2009
Location: Manchester, UK
Posts: 1,030
Send a message via MSN to ZunePod Send a message via Yahoo to ZunePod
Default

Although this is for Mac, it should be the same on iPhone:

Save struct to NSDictionary - Dev Archives
ZunePod is offline   Reply With Quote
Old 02-15-2010, 09:10 AM   #3 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 83
Default

Hi, thanks for the info, I already came across that forum post, it doesn't really help me if I'm honest, I think I have isolated my problem however, it seems storing Dictionaries as objects inside a dictionary is no problem, the problem is only when I try to store a struct, and I think it's something to do with he memory of the struct.

Here is a sample of what I am doing:

This is my struct
Code:
typedef struct{
	NSMutableString *value;
	NSDictionary *attributes;
} xmlElement;
In a method of my class I have:
Code:
	xmlElement elem;
	elem.value = nodeValue;
	elem.attributes = nodeAttributes;
	[nodeValues setObject:elem forKey:xmlPath];
* nodeValues is a NSMutableDictionary and xmlPath is an NSMutableString

I think that maybe, I need to specify retain for the elem ?

<SOLUTION>
I had to convert the C-struct to objectif C with the NSValue class, and use getValue on that to retrieve the struct.

Last edited by djp_phillips; 02-15-2010 at 03:54 PM. Reason: Found solution
djp_phillips is offline   Reply With Quote
Reply

Bookmarks

Tags
nsarray, nsdictionary, storing, struct

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: 355
22 members and 333 guests
ADY, Dani77, Duncan C, e2applets, Grinarn, HemiMG, Herbie, JasonR, keeshux, linkmx, macquitzon216, mer10, Monstertaco, prchn4christ, Promo Dispenser, Robiwan, sebasx, sly24, Touchmint, twerner, zulfishah
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,881
Threads: 89,228
Posts: 380,760
Top Poster: BrianSlick (7,129)
Welcome to our newest member, macquitzon216
Powered by vBadvanced CMPS v3.1.0

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