Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa touch 为什么NSAssert1等而不是NSAssert?_Cocoa Touch_Cocoa_Nsassert - Fatal编程技术网

Cocoa touch 为什么NSAssert1等而不是NSAssert?

Cocoa touch 为什么NSAssert1等而不是NSAssert?,cocoa-touch,cocoa,nsassert,Cocoa Touch,Cocoa,Nsassert,我认为NSAssert不能使用printf说明符,但是: NSAssert(0, @"%@%@", @"foo", @"bar"); 正如您所期望的那样: *** Assertion failure in -[MyClass myMethod], <Path>/MyClass.m:84 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'foob

我认为
NSAssert
不能使用
printf
说明符,但是:

NSAssert(0, @"%@%@", @"foo", @"bar");
正如您所期望的那样:

*** Assertion failure in -[MyClass myMethod], <Path>/MyClass.m:84
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
    reason: 'foobar'
***-[MyClass myMethod],/MyClass.m:84中的断言失败
***由于未捕获异常“NSInternalInconsistencyException”而终止应用程序,
原因:“foobar”
那么当
NSAssert
工作时,使用
NSAssert1
NSAssert2
等有什么意义呢


这与Xcode 4.0和iOS 4.3 SDK密切相关。(如果没有,我将更新标记。)

当前版本的
NSAssert()
使用预处理器变量宏,即
\u VA\u ARGS\u
。由于可变宏是C99的一项功能,我猜较早版本的SDK不允许在
NSAssert()
中使用可变参数,因此需要
NSAssert1()
NSAssert2()

如果您试图编译

NSAssert(0, @"%@%@", @"foo", @"bar");
使用
-std=c89
-ansi
(ISO C90,不支持可变宏的较旧版本的C),会出现编译器错误:

error: too many arguments provided to function-like macro invocation
    NSAssert(0, @"%@%@", @"foo", @"bar");
要使用
-std=c89
-ansi
编译该代码,需要使用
NSAssert2()


巴伐利亚的回答很好

只是加上我的一点。 对于那些面临问题的人来说,
提供了太多的参数,无法像宏调用那样工作
。 请注意@BAVariable提到的
-std=c89

下面是我如何解决这个问题的

  • 转到构建设置->苹果LLVM 6.1
  • 查找C语言方言
  • 更改为
    -std=c99
  • NSAssert2(0, @"%@%@", @"foo", @"bar");