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 > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-23-2010, 10:58 AM   #1 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 26
martin is on a distinguished road
Default Getting values from an array of wrapped structs using NSValue

Hi guys!

I am trying to get back a value that I put in an array of structs wrapping these structs using NSValue.

I have these on my myAppDelegate.h :
Code:
typedef struct {
	NSInteger	ticketId;
	NSString	*eventTitle;
	NSString	*eventInfo;
} TicketDataStruct;

@interface myAppDelegate : NSObject <UIApplicationDelegate> {
	[...]
	NSMutableArray *dataArray;
	[...]
}
[...]
@property (nonatomic, retain) NSMutableArray *dataArray;
[...]
then, in myAppDelegate.m:
Code:
@synthesize dataArray;

#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
	
	TicketDataStruct str;
	str.ticketId = 543;
	str.eventTitle = @"My event";
	str.eventInfo = @"My event info";
	
	NSValue *myValue = [NSValue value:&str withObjCType:@encode(TicketDataStruct)];
	
	dataArray = [[NSMutableArray alloc] initWithObjects:nil];
	[dataArray addObject:miValor];
	[...]
}
Now, How can I get access to the values of the structs?
I am trying with this, but I am getting nil (This code is inside (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath):
Code:
	[...]
	myAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
	TicketDataStruct retVal;
	NSValue *t = [[NSValue alloc] initWithBytes:[delegate.dataArray objectAtIndex:indexPath.row] objCType:@encode(TicketDataStruct)];
	[t getValue:&retVal];
	topLabel.text = [[NSString alloc] initWithString:retVal.eventTitle];
	[...]
But it doesn't work. I am getting nil in retVal.eventTitle.

Can you help me?
Thanks!
martin is offline   Reply With Quote
Old 06-24-2010, 08:27 AM   #2 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 26
martin is on a distinguished road
Default

Ok, to make it easier...
I need to get back values from a struct wrapped inside a NSValue.
Any ideas?

Thank you
martin is offline   Reply With Quote
Old 06-30-2010, 02:16 PM   #3 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 2
dmf1978 is on a distinguished road
Default

Forget about structs. That is plain wrong!!
Instead, write classes. Then, create objects based on the class and add these to the NSMutableArray.
Now, you can access to the members like this:
Code:
MyClass *myObject = [[MyClass alloc] init];
myObject = [delegate.dataArray objectAtIndex:indexPath.row];
topLabel.text = myObject.eventName;
dmf1978 is offline   Reply With Quote
Old 06-30-2010, 08:57 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by dmf1978 View Post
Forget about structs. That is plain wrong!!
Instead, write classes. Then, create objects based on the class and add these to the NSMutableArray.
Now, you can access to the members like this:
Code:
MyClass *myObject = [[MyClass alloc] init];
myObject = [delegate.dataArray objectAtIndex:indexPath.row];
topLabel.text = myObject.eventName;
It's not wrong. It's old school, and not very OOP, but it should work, and sometimes this approach ends up being the right solution, even if it isn't very elegant or flexible. Life is full of trade-offs and compromises...


To the OP, I didn't see anything obvious wrong with your code. Then again, it's almost 10:00 pm here, and I had a beer with dinner. My mental focus is getting a little soft...
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 08-11-2010, 01:54 AM   #5 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 20
sanniv is on a distinguished road
Default memory leak in the code...

Quote:
Originally Posted by dmf1978 View Post
Forget about structs. That is plain wrong!!
Instead, write classes. Then, create objects based on the class and add these to the NSMutableArray.
Now, you can access to the members like this:
Code:
MyClass *myObject = [[MyClass alloc] init];
myObject = [delegate.dataArray objectAtIndex:indexPath.row];
topLabel.text = myObject.eventName;
Just use this:
MyClass *myObject = [delegate.dataArray objectAtIndex:indexPath.row];
topLabel.text = myObject.eventName;

other wise you will lead to memory leak....
sanniv is offline   Reply With Quote
Reply

Bookmarks

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: 324
7 members and 317 guests
anothermine, Chickenrig, Domele, givensur, michaelhansen, PixelInteractive, Sloshmonster
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,892
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

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