Francis F
Francis F

Reputation: 3285

Implicit boolean conversion of Objective-C object literal always evaluates to true ios

I recently downloaded Xcode 9.2 and updated my project settings to recommended. Now I'm getting this warning in my code for all the places where I've used assert for eg:

assert(@"Must be implemented by subclass");

What is the proper alternative for this?

Upvotes: 0

Views: 1739

Answers (3)

Daij-Djan
Daij-Djan

Reputation: 50089

a) assert needs a condition to check. you treat the string as a condition and it is non-null... do

b) you use a NSString and not a char* so you pass objC object to C which wont work in this case either ... do

assert(codition & "Message");

c) you should use NSAssert for ObjC Code .. do

NSAssert(codition, @"Message");

Upvotes: 0

Jeffery Thomas
Jeffery Thomas

Reputation: 42588

If you are using Objective-C, you want to call NSAssert(), not assert() (which is a C function).

NSAssert(NO, @"Must be implemented by subclass");

If you want to continue using assert(), you should treat it like a C function.

assert(0); // <-- Note: no message is provided

You might get away with

assert(/* Must be implemented by subclass */ 0);

or

assert("Must be implemented by subclass" == NULL); // <-- Note: No `@`

Upvotes: 2

Flexicoder
Flexicoder

Reputation: 8501

Your assert condition is a string which will always pass the test.

You should be checking a condition in the assert

Upvotes: 1

Related Questions