if NSString does not equal function? - objective-c

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
}

Related

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:#""].

Parsing json incorrect

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"])

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
}

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

Comparing Strings in Cocoa

I have tried:
- (NSString*) generateString
{
NSString* stringToReturn = #"thisString";
return stringToReturn;
}
- (void) otherMethod
{
NSString *returnedString = [self generateString];
if (returnedString == #"thisString")
{ // Do this }
else if (returnedString == #"thatString")
{ // Do that }
}
Which never matches.
I have then tried
if ([returnedString compare:#"thisString"] == 1)
But the compare method always returns 1 for me, even when comparing with a different string.
What is the correct way to do this, and what result should I expect?
First of all, you are using the == operator to compare two object pointers (of type NSString *). So that returns true when the pointers are the same, not when the strings have the same contents. If you wanted to compare whether two strings are the same, you should use isEqualToString: or isEqual: (isEqual: is more general as it works for all types of objects).
Second, compare: returns 0 (NSOrderSame) when they are the same, and 1 (NSOrderedDescending) when the first is greater than the second. So in fact it returns 1 only when they are different (specifically, when the first is greater than the second).
[returnedString isEqualToString: #"thisString"]
When comparing two identical strings compare will return NSOrderedSame, which is 0. It can also return NSOrderedAscending, -1, and NSOrderedDescending, 1.
You may prefer to use isEqualToString which returns YES or NO.
if ([returnedString isEqualToString:#"thisString"])
NSLog(#"Equal");
else
NSLog(#"Not Equal");
The == operator when applied to objects check whether the pointers are equal.
You have to use -compare method or one of its companions -compare:options: -compare:options:range: or -compare:options:range:locale: which return a NSComparisonResult (NSOrderAscending, NSOrderSame, NSOrderDescending)
If you just need equality comparison, you can use -isEqualToString: which returns a BOOL value.