Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Transform string to variable name

mayhem_nzmayhem_nz Posts: 34Registered Users
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


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


NSArray *array = [[NSArray alloc] initWithObjects:@\"item1\", @\"item2\", nil];
[self logVariables:array];



Code of the method

- (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
Post edited by mayhem_nz on

Replies

Sign In or Register to comment.