Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird error NSAssert

I can't figure out why I get

use of undeclared identifier _cmd  did you mean rcmd

on the line where NSAssert is.

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int x = 10;

    NSAssert(x > 11, @"x should be greater than %d", x);

    [pool drain];
    return 0;
}
like image 942
foho Avatar asked Mar 16 '12 14:03

foho


3 Answers

Inside every Objective-c method there are two hidden variables id self and SEL _cmd

so

- (void)foo:(id)bar;

is really

void foo(id self, SEL _cmd, id bar) { ... }

and when you call

[someObject foo:@"hello world"]

it is actually

foo( someObject, @selector(foo), @"hello world")

If you cmd-click on NSAssert to jump to it's definition you will see that it is a macro that uses the hidden _cmd variable of the method you are calling it from. This means that if you are not inside an Objective-c method (perhaps you are in 'main'), therefore you don't have a _cmd argument, you cannot use NSAssert.

Instead you can use the alternative NSCAssert.

like image 78
hooleyhoop Avatar answered Nov 20 '22 03:11

hooleyhoop


NSAssert is only meant to be used within Objective-C methods. Since main is a C function, use NSCAssert instead.

like image 44
highlycaffeinated Avatar answered Nov 20 '22 01:11

highlycaffeinated


Try to replace

NSAssert(x > 11, [NSString stringWithFormat:@"x should be greater than %d", x]);

with

NSCAssert(x > 11, [NSString stringWithFormat:@"x should be greater than %d", x]);

like image 1
arun.s Avatar answered Nov 20 '22 02:11

arun.s