Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixing "direct comparison of a string literal has undefined behavior" automatically

In Xcode, I'm getting the error "direct comparison of a string literal has undefined behavior," and I know why I'm getting it, but is there some way for me to click a button and have Xcode remove it? I'm saying this because in 370 places in my app I've gotten it.

like image 573
Someone Avatar asked Apr 26 '13 23:04

Someone


2 Answers

The clang option to disable this warning is -Wno-objc-literal-compare.

However, warnings are there for a reason; this one is because comparing against NSString literals using == is not guaranteed to behave as you might expect. Use isEqual: or isEqualToString: instead and you can both get rid of this warning and avoid having this turn into a bug for you later.

like image 139
rickster Avatar answered Oct 23 '22 07:10

rickster


You can avoid the warning using `isEqualToString` instead of `==`.        
`==` simply compares the pointers, which will usually be different even   
if their contents are the same. The`isEqualToString` method compares   
their contents.
like image 25
Sourabh Kumbhar Avatar answered Oct 23 '22 06:10

Sourabh Kumbhar