1

I use this method to access my mysql database from Xcode:

NSString *URL = [NSString stringWithFormat:@"http://test.test:8888/test/loadUserData.php?username=%@", userName];

NSString *rawJSON = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:URL]];

const char *convert = [rawJSON UTF8String]; NSString *responseString = [NSString stringWithUTF8String:convert];

if ([rawJSON length] == 0) { [rawJSON release];

}

SBJsonParser *parser = [[SBJsonParser alloc] init];

userInfo = [[parser objectWithString:responseString error:nil] copy]; SSN = [userInfo objectAtIndex:0];

[parser release];

return userInfo;

Everything works great. EXCEPT that I can't compare strings in the result with normal nsstrings. If I say

if ([userinfo objectAtIndex:0) == @"Dan") { ..do something }

Xcode never sees that it is the same value.. I don't know if there is something wrong with the format (My database is UTF-8) And how can I convert the result so xCode can compare the response with NSStrings?

Thanks!

1
  • 1
    What does this have to do with Xcode? Commented May 16, 2011 at 16:07

2 Answers 2

1

== does not compare the value of strings, it just compares their addresses.

Use

if ([[userinfo objectAtIndex:0] isEqualToString:@"Dan"]) { ... } 

or something similar instead.

Sign up to request clarification or add additional context in comments.

2 Comments

One way to think of it is as - object comparison; your original code was asking, "Is object A equal to object B?". So, instead think, "Are the contents of object A the same as the contents of object B?" Don't forget @"A String" is not actually a string literal it's shorthand for an instance of NSString with contents 'A String'; i.e. an object in it's own right.
@user753126 Yes, == compares the pointers/addresses. The variables you throw at == are pointers, i.e. NSString *. So for comparison you always want something like isEqualXXX etc.
1

If you know for sure that you have string data on both sides of the condition you could use

if ([userinfo objectAtIndex:0] isEqualToString:@"Dan") {
    // summat...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.