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
下面是我如何解决这个问题的
-std=c99
NSAssert2(0, @"%@%@", @"foo", @"bar");