Ste*_*her 7 cocoa cocoa-touch nsassert
我以为NSAssert不能使用printf说明符,但是这个:
NSAssert(0, @"%@%@", @"foo", @"bar");
Run Code Online (Sandbox Code Playgroud)
像你期望的那样工作:
*** Assertion failure in -[MyClass myMethod], <Path>/MyClass.m:84
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'foobar'
Run Code Online (Sandbox Code Playgroud)
那么,有什么用点NSAssert1,NSAssert2等时NSAssert的作品?
如果这很重要,可以使用Xcode 4.0和iOS 4.3 SDK.(如果没有,我会更新标签.)
小智 18
当前版本的NSAssert()使用预处理器可变参数宏,即__VA_ARGS__.由于复杂的宏是C99的功能,我的猜测是,旧版本的SDK中并没有允许可变参数NSAssert(),因此需要NSAssert1(),NSAssert2()等等.
如果你试图编译
NSAssert(0, @"%@%@", @"foo", @"bar");
Run Code Online (Sandbox Code Playgroud)
使用-std=c89或-ansi(ISO C90,不支持可变参数宏的旧版C),会出现编译错误:
error: too many arguments provided to function-like macro invocation
NSAssert(0, @"%@%@", @"foo", @"bar");
Run Code Online (Sandbox Code Playgroud)
要使用-std=c89或编译该代码-ansi,您需要使用NSAssert2():
NSAssert2(0, @"%@%@", @"foo", @"bar");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2403 次 |
| 最近记录: |