Parsing json incorrect - objective-c

i'm parsing something from the Apple JSON (the rating of the app) and i tried something like:
if ([StoreParse objectForKey:#"averageUserRating"] == #"4.5") {
NSLog(#"xx");
} else {
NSLog(#"xxx");
}
The App has a rating of 4.5 and if i do
NSlog (#"%#", [StoreParse objectForKey:#"averageUserRating"]);
The output is : 4.5
but when i run the script the NSlog in the first code's output is "xxx" does anyone can help me?

Comparing strings, which are essentially pointers to instances of the NSSring class, is erroneous, as two identical-content strings can have a different memory address. Use
if ([[StoreParse objectForKey:#"averageUserRating"] isEqualToString:#"4.5"])
instead.

Use isEqualToString:
if ([[StoreParse objectForKey:#"averageUserRating"] isEqualToString:#"4.5"]) {
NSLog(#"xx");
}

You can't do this:
if ([StoreParse objectForKey:#"averageUserRating"] == #"4.5")
You need to do:
if ([[StoreParse objectForKey:#"averageUserRating"] isEqualToString:#"4.5"])
That's assuming it's a string. If it's an NSNumber then do:
if ([[StoreParse objectForKey:#"averageUserRating"] floatValue] == 4.5f)
(Although be careful about comparing equality of floats)
See this question for more information on string equality.

If the value for averageUserRating is an NSNumber, you should convert it to a formatted NSString first then compare it to the #"4.5" string literal:
if ([[NSString stringWithFormat:#"%1.1f", [[StoreParse objectForKey:#"averageUserRating"] floatValue]] isEqualToString:#"4.5"])

Related

if NSString does not equal function?

I have searched all over the place for this, including apple's docs on NSString (maybe I didn't see?) but I am trying to find a method in xCode for checking wether a NSString does not equal to something. Much like
if (myNSSting = #"text" {...
except specifically I want to check if it does not equal to 'text'.
if(![myNSString isEqualToString:#"text"])
For case insensitive compare we can handle by:
if( [#"Some String" caseInsensitiveCompare:#"some string"] == NSOrderedSame ) {
// strings are equal except for possibly case
}

Objective-C string comparison

I have an array of names but can't seem to make the comparison work. Do I have an improper use of the language here?
NSLog(#"%#",[arrayOfNames objectAtIndex:0]);
if ([arrayOfNames objectAtIndex:0] == "Blue"){
NSLog(#"it's Blue");
}
else {
NSLog(#"it's not Blue");
}
The output is the following one:
Blue
it's not Blue
Use the following:
if ([[arrayOfNames objectAtIndex:0] isEqualToString:#"Blue"])
You're comparing two objects (one of the id-type, the other is a C-string) with the == operator. The comparison will fail, since they are 2 different objects. With the isEqualToString you are comparing the value of the object to the string #"Blue".

How to best compare two NSString objects while ignoring case?

I want to compare two strings. It fails when the string have capital letter. How do I convert both string to capitalize and compare.
I have a sample code, can someone correct this.
if ([[txtAnswer.text capitalizedString] isEqualToString:[answer capitalizedString]]) {
// Do somehing
}
If you look at the NSString class reference you will see under the heading Identifying and Comparing Strings the methods caseInsensitiveCompare: and localizedCaseInsensitiveCompare:.
You might try something like:
if ([txtAnswer.text caseInsensitiveCompare: answer] == NSOrderedSame) {
// do something.
}
You can do a case insensitive string compare.
if([txtAnswer.text compare:answer options:NSCaseInsensitiveSearch] == NSOrderedSame)
{
// Do somehing
}

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);
}

String compare Objective-C

I've been struggling with a simple comparison but I can't get it to work.
I´m reading a XML file and I need to compare data from it in order to show the right picture.
http://www.cleaner.se/larm.xml (Example file for parsing)
I have tried things like:
if([aLarm.larmClass isEqualToString:#"A"])
NSLog(#"same");
else
NSLog(#"Not same");
If I use: NSLog(aLarm.larmClass); console puts it out nicely as it should. What am I doing wrong?
You can use the NSString compare: methods. For example:
if ([myString caseInsensitiveCompare:#"A"] == NSOrderedSame ) {
NSLog(#"The same");
} else {
NSLog(#"Not the same.");
}
The result is an NSComparisonResult which is just an enum with types NSOrderedSame, NSOrderedAscending and NSOrderedDescending.
Check the documentation on the various compare: methods here.
Of course, if the receiver is actually an NSString, then isEqualToString: should also work. So if you're trying to compare a class name (aLarm.larmClass ??), then you can call:
if ([NSStringFromClass([aLarm class]) isEqualToString:#"A"] ) {
NSLog(#"The same");
}
If the larmClass property is a string, make sure that it is actually one character in length (i.e. it doesn't have any leading or trailing whitespace that was accidentally included when parsing the XML). If the larmClass property truly is an NSString containing the letter ‘A’ then [aLarm.larmClass isEqualToString:#"A"] will return YES.
Do a:
NSLog(#"%u, %#", [aLarm.larmClass length], aLarm.larmClass);
and just make sure that it shows “1, A”.