How to check if [request responseString] is equal to some other string? - objective-c

For example [request responseString]'s value sent by my servlet to my iphone application is "myinfo". In my iphone application, I made a string like this:
NSString *str = #"myinfo";
then i have if else
if([responseString isEqualToString:str]){
NSLog(#"condition true");
}else{
NSLog(#"condition false");
}
in console its always showing "condition false". Whats the problem? Isn't isEqualToString is write method to check if strings are equal or not? Thanks in advance.

Howwever much you think your two strings are completely equal, they are not. Believe me, if -isEqualToString: did not return YES for two equal strings, somebody would have noticed in the 20 odd years it has been part of the Cocoa API.
I suspect that one of your two strings contains some non printing characters. You might have a line feed or a space or a tab in it. Another possibility (one that I came across recently) is that, for certain character set encodings, you can create an NSString with a nul character in it. If it's at the end, it won't show up. Try logging the lengths of the two strings, or converting them to NSData objects using the UTF16 encoding and logging them.

The NSString method isEqualToString is the correct thing to use here. You can do a sanity check by adding a log to your method:
NSLog(#"responseString = %#",responseString);
NSLog(#"str = %#",str);
if([responseString isEqualToString:str]){
NSLog(#"condition true");
}else{
NSLog(#"condition false");
}
Remember that NSStrings are Case Sensitive, so the two strings must appear exactly the same.

Since you said you're using connectios, sometimes the data retrieved by web is weird, you should first encode the data in a string and then NSLog it to see if it has special characters.
NSString *response = [[NSString alloc] initWithData:dataRetrieved encoding:NSUTF8StringEncoding];
Then you can make sure if your [request responseString] is not getting special chars.

The better way to compare strings in ios:
NSString *responseString = <your string>;
NSString *string2 = <your string>;
if ([responseString caseInsensitiveCompare:string2] == NSOrderedSame) {
//strings are same
} else {
//strings are not same
}

Related

Having trouble taking an index of an array and making it an NSString

I get an array from a JSON and I parse it into an NSMutableArray (this part is correct and working). I now want to take that array and print the first object to a Label. Here is my code:
NSDictionary *title = [[dictionary objectForKey:#"title"] objectAtIndex:2];
arrayLabel = [title objectForKey:#"label"];
NSLog(#"arrayLabel = %#", arrayLabel); // Returns correct
//Here is where I need help
string = [arrayLabel objectAtIndex:1]; //I do not get the first label (App crashes)
NSLog(#"string = %#", string);
other things that I have already tried are as follows:
string = [NSString stringWithFormat:#"%#", [arrayImage objectAtIndex:1]];
and
string = [[NSString alloc] initWithFormat:#"%#", [arrayImage objectAtIndex:1]];
Any help is greatly appriciated!
EDIT: The app does not return a single value and crashes.
Your code doesn't match the structure of your JSON. In your comment on the deleted answer, you said you got an exception when sending objectAtIndex: to an NSString. In your case, arrayLabel isn't an array when you think it is.
If your JSON has an object, your code needs to treat it as an NSDictionary. Likewise for arrays and NSArray and strings and NSString.
In addition to whatever else was going on, you repeatedly refer to "first" but use the index 1. In most C-based programming languages (and others, as well) the convention is that indexes into arrays are 0-based. So, use index 0 to get the first element.

How to check if NSString returned by objectForKey is "" objective c

I'm not exactly sure how to check whether a NSString is blank or not, I've got this code...
NSString *imageName = [myItem objectForKey:#"iconName"];
if(imageName == #"")
{
}
And when I do a print on the myItem object, it comes up as..
iconName = "";
At the NSString *imageName line, I noticed in xcode in the console it says
"variable is not NSString"
Which I don't get as iconName is saved and stored on the parse.com database as a NSString.
When I run that code though it doesn't seem to realise that imageName = "";
You should use this code block when comparing strings:
if ([imageName isEqualToString:#""]){
}
You need to use isEqualToString to compare two strings. If you just use == then you are comparing two pointers.
You could also check to see if the object you are receiving is a NSString by:
if ([imageName isKindOfClass:[NSString class]])
Hope this helps.
Although you have a few answers already, here is my take.
First of all, your warning (not error) can be fixed like this:
NSString *imageName = (NSString *)[myItem objectForKey:#"iconName"];
Then, I would check to make sure that the string is not nil and that it is not blank. The easiest way to do this in objective-C is to check the length of the string, since if it nil it will return 0, and if it is empty, it will return 0:
if([imageName length] == 0)
{
// This is an empty string.
}
As #jlehr points out, if there is the possibility that imageName may not actually be stored as a string, then in order to prevent a crash you need to check first. (This may or may not be needed, depending on the logic of your application):
if ([imageName isKindOfClass:[NSString class]]
{
if([imageName length] == 0)
{
// This is an empty string.
}
}
The "variable is not NSString" is probably because objectForKey: return an id.
To should use [imageName isEqualToString:#""].

Comparing the values of two nsstrings

So I have been trying to compare two NSStrings in xcode. However, it is not working. What am I doing wrong?
NSString Prog are characters that are xml parsed from mysql
char *cStr = "YES";
NSString *str3 = [NSString stringWithUTF8String:cStr];
if ([str3 isEqualToString:prog]) {
[switch1 setOn:YES animated:YES];
}
else {
[switch1 setOn:NO animated:YES];
}
-[NSString isEqualToString:] is normally what you would use, and what you are using in such a scenario. Ensure that prog is a valid NSString (e.g. the correct type and not nil), and keep in mind that string comparison in this implementation is case-sensitive -- i.e. "Yes" would not be equal to "YES".
use the following methods to compare two NSStrings :
[yourString isEqualToString:#"testString"]

UTF8String problem

I am facing a strange problem with this UTF8String:
parentMode = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
…
if(parentMode != #"Sleep")
{
NSLog(#"%s", [parentMode UTF8String]);
}
My questions are:
Why do I have to do this conversion in order to log parentMode?
The log is printing Sleep. So how is that if is done anyway?
You can't compare strings using the normal relational operators, you must use:
if (![parentMode isEqualToString:#"Sleep"])
{
NSLog (#"%#", parentMode);
}
You may want to check that parentMode is not nil before using that method, however. You don't need to use the UTF8String method, you can log the string directly using the %# format specifier. If this is not working, then there is something very important that you are omitting from the code you provided.
To log the string, you can write:
NSLog(#"%#", parentMode);
Using the %# placeholder, there's not need to convert it back to UTF-8.
This probably also explains why the if statement works.
Update:
You should compare string with isEqualToString:
[parentMode isEqualToString: #"Sleep"]
If you are comparing the integers then you have to use the syntax whatever you have used in the post.But when you compare the strings use this.
if (![parentMode isEqualToString:#"Sleep"])
{
NSLog (#"%#", parentMode);
}

Need formatting help for isEqualToString args

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?
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.