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-22-2009, 11:30 AM   #1 (permalink)
iNoob
 
Join Date: Jan 2009
Location: UK
Posts: 34
Default Transform string to variable name

I want to be able to pass an array of variable names to a method which will then run though a loop and display the value of an objects variable I have created in the NSLog.


This is the standard way I have been printing each variable of the object info to NSLog

Code:
NSLog(@"%i. item1: %d", 0, [[[mainDelegate.itemArray objectAtIndex:0] item1] integerValue]);

NSLog(@"%i. item2: %d", 0, [[[mainDelegate.itemArray objectAtIndex:0] item2] integerValue]);
But as there are 15 of them I didn’t want to keep filling up the NSLog with them all. I was thinking that if would be better to just display the ones related to any changes made to them. So I started to make a method that I could pass an array with the names of each variable as a string.

Code to call the method
Code:
NSArray *array = [[NSArray alloc] initWithObjects:@"item1", @"item2", nil];
[self logVariables:array];
Code of the method
Code:
- (void) logVariables:(NSArray *)anArray {
	XAppDelegate *mainDelegate = (XAppDelegate *)[[UIApplication sharedApplication] delegate];
	
	for(int i = 0; i < [anArray count]; i++) {
		
		NSString *variableName = [anArray objectAtIndex:i];

		NSLog(@"%i. %@:	%d", 0, saveVariable, [[[mainDelegate.itemArray objectAtIndex:0] variableName] integerValue]); //error here on variableName

	}	
}
Whatever I do I can’t get it to accept the string variableName in the NSLog as the variables name.

Any ideas would be greatly appreciated.
mayhem_nz
mayhem_nz is offline   Reply With Quote
Old 02-22-2009, 11:43 AM   #2 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 429
Default

Someone mentioned earlier that you can use NSDictionaries in lieu of "eval" functionality. Try searching the forum for that.
lbendlin is offline   Reply With Quote
Old 02-22-2009, 12:31 PM   #3 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
Default

Why don't you store the integers in an NSArray?
PhoneyDeveloper is offline   Reply With Quote
Old 02-22-2009, 12:57 PM   #4 (permalink)
iNoob
 
Join Date: Jan 2009
Location: UK
Posts: 34
Default

Quote:
Originally Posted by lbendlin View Post
Someone mentioned earlier that you can use NSDictionaries in lieu of "eval" functionality. Try searching the forum for that.
I searched the forums and I believe the thread you are talking about is http://www.iphonedevsdk.com/forum/ip...nto-cocoa.html
Looking into it now.

Quote:
Originally Posted by PhoneyDeveloper View Post
Why don't you store the integers in an NSArray?
That is another way of doing it. Not all of the objects variables are integers but I'm sure I could get this to work if need be. I was hoping to simply call the objects variables by name and having the code quite simplistic.
mayhem_nz is offline   Reply With Quote
Old 02-22-2009, 01:48 PM   #5 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
Default

This question also came up yesterday

http://www.iphonedevsdk.com/forum/ip...able-name.html
PhoneyDeveloper is offline   Reply With Quote
Reply

Bookmarks

Tags
string, transform, variable name

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
» 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:15 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0