Objective c 奇怪的错误NSAssert
我不明白为什么我会这样Objective c 奇怪的错误NSAssert,objective-c,nsassert,Objective C,Nsassert,我不明白为什么我会这样 use of undeclared identifier _cmd did you mean rcmd 在NSAssert所在的线路上 #import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; int x = 10;
use of undeclared identifier _cmd did you mean rcmd
在NSAssert所在的线路上
#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;
}
#导入
int main(int argc,const char*argv[]
{
NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init];
int x=10;
NSAssert(x>11,@“x应大于%d”,x);
[泳池排水沟];
返回0;
}
如果要使用格式参数,必须将字符串包装在NSString类中。这是因为@“
是普通NSString的默认构造函数。现在,它的编写方式为NSAssert
函数提供了第三个参数,并对其进行了处理
NSAssert(x > 11, [NSString stringWithFormat:@"x should be greater than %d", x]);
试着替换
NSAssert(x>11,[NSString stringWithFormat:@“x应大于%d”,x])
与
NSCAssert(x>11,[NSString stringWithFormat:@“x应大于%d”,x])
NSAssert
。由于main
是一个C函数,因此使用nscaster
代替。在每个Objective-C方法中都有两个隐藏变量id self
和SEL\u cmd
所以
真是
void foo(id self, SEL _cmd, id bar) { ... }
当你打电话的时候
[someObject foo:@"hello world"]
实际上是
foo( someObject, @selector(foo), @"hello world")
如果你点击NSAssert跳转到它的定义,你会看到它是一个宏,它使用了你调用它的方法的hidden _cmd变量。这意味着,如果您不在Objective-c方法中(可能在“main”中),因此没有_cmd参数,就不能使用NSAssert
相反,您可以使用替代NSCAssert。TL;DR-坚持迷路的NSAssert()-不要在生产中尝试此操作 原始代码
#导入
int main(int argc,const char*argv[]
{
NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init];
int x=10;
NSAssert(x>11,@“x应大于%d”,x);
[泳池排水沟];
返回0;
}
构建失败
Compiling file hello.m ...
hello.m:9:5: error: use of undeclared identifier '_cmd'
NSAssert(x > 11, @"x should be greater than %d", x);
^
/usr/include/Foundation/NSException.h:450:32: note: expanded from macro 'NSAssert'
handleFailureInMethod: _cmd \
^
hello.m:9:5: error: use of undeclared identifier 'self'
/usr/include/Foundation/NSException.h:451:17: note: expanded from macro 'NSAssert'
object: self \
^
2 errors generated.
基于和
,
如果我坚持使用,以下肮脏的黑客可能适用
而不是
#导入
int main(int argc,const char*argv[]
{
NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init];
int x=10;
//肮脏的黑客
SEL _cmd=NULL;
NSObject*self=NULL;
NSAssert(x>11,@“x应大于%d”,x);
[泳池排水沟];
返回0;
}
构建和运行
Compiling file hello.m ...
Linking tool hello ...
2021-03-04 21:25:58.035 hello[39049:39049] hello.m:13 Assertion failed in (null)(instance), method (null). x should be greater than 10
./obj/hello: Uncaught exception NSInternalInconsistencyException, reason: hello.m:13 Assertion failed in (null)(instance), method (null). x should be greater than 10
万岁,它工作了!但是,唉,请远离它:)如果我将代码更改为NSAssert(x>11,@“x应该大于”);来自@highlycaffined的答案就是我刚刚得到的答案。非常好的解释。。。我也学到了一些关于objective-C的东西!
Compiling file hello.m ...
Linking tool hello ...
2021-03-04 21:25:58.035 hello[39049:39049] hello.m:13 Assertion failed in (null)(instance), method (null). x should be greater than 10
./obj/hello: Uncaught exception NSInternalInconsistencyException, reason: hello.m:13 Assertion failed in (null)(instance), method (null). x should be greater than 10