tags:

views:

58

answers:

1

Code sample:

NSString *title = [[NSString alloc]initWithFormat: @"%@", [self.answers valueForKey:idVor]];
NSString *message = [[NSString alloc]initWithFormat: @"%@",nameVor];

NSLog(@"%@", title);
NSLog(@"%@", message);

if([title isEqualToString:message])
    NSLog(@"equal");

The vars title and message never respond to the if statement even though they contain the same string.

I ran NSLogs for these to see what was contained in each string var.
I got the following output:

f[Session started at 2009-09-21 17:27:56 -0500.]
2009-09-21 17:28:00.256 pickerReview[2394:20b] (
    Amedee
)


2009-09-21 17:28:00.257 pickerReview[2394:20b] Amedee

I guess it's not equal because the NSString title var has parentheses around it... Is there a way to format this so that it satisfies the expression in the if statement?

+3  A: 

The issue appears to be that you're asking self.answers (an NSArray) for its valueForKey:@"whatever" — this doesn't return a string, but an array made up of the result of asking each object in the array for that key value. NSArray's description method (what gets printed when you NSLog it) is the contents of the array surrounded by parentheses. So you're comparing a string to an array containing a string.

Chuck
I was trying to format the outputs to boldface with double asterisks, not sure what happened. Anyways... the title var is a string contained in an NSDictionary value and the other is a string object inside an array. They were both input simply as @"Amedee". Why does the dictionary value output with parentheses?
samfu_1
I see now that I'm thinking two things are equal when they aren't. What is a possible solution to put the [self.answers valueForkey: idVor] into a format that IS equal to my NSString object?
samfu_1
Well, you have an array containing a string, so you need to get the string out of it.
Chuck
I understand that I need to get the string out of the array, and I'm still asking how to do that. I've tried several things...I've tried naming a variable, I've tried, init'ing a whole new string, but I still get the format of parentheses first (still getting the array returned)NSString *idToCompare = ??? [self.answers valueForKey: idVor];
samfu_1
You ask the array for an object: `[array objectAtIndex:0]`
Chuck
Thank you very much, worked perfectly. I was getting so caught up with the mindset that the object in the array was a string and ignoring the fact that the method I was using to get that object was returning an array.... lesson learned.
samfu_1