我不明白为什么我会

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;
}

推荐答案

在每个Objective-c方法中都有两个隐藏变量id selfSEL _cmd

所以

- (void)foo:(id)bar;

真的

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

当你打电话的时候

[所以meObject foo:@"hello world"]

事实上

foo( 所以meObject, @selector(foo), @"hello world")

如果你点击NSAssert跳转到它的定义,你会看到它是一个宏,它使用了你调用它的方法的隐藏的_cmd变量.这意味着,如果您不在Objective-c方法中(可能在"main"中),因此没有_cmd参数,就不能使用NSAssert.

相反,你可以使用另一种方法.

Objective-c相关问答推荐

将 NSMutableAttributedString 转换为 NSString

iOS9 - 这个应用程序正在从后台线程修改自动布局引擎 - 在哪里?

当我们只能使用 NSObject 时,为什么还要使用 id?

协议与类别

为什么我不应该在 init/dealloc 中使用 Objective C 2.0 访问器?

iOS 应用程序下一个键不会转到下一个文本字段

如何使用键值编码判断对象是否存在键?

ios 13 - 带有 UISearchBar _searchField 的自定义 SearchBar 不起作用

如何从 Xcode4 生成 UML 图

在 Xcode 中打破 EXC_BAD_ACCESS?

我们可以在 UIPageViewController 中自定义页面指示器吗?

如何确定 UICollectionView flowLayout 中单元格之间的间距

如何在 Objective-C 中创建一个空白的透明 png?

UIWebView didFinishLoading 多次触发

如何在嵌入 UIViewController 的 UITableView 中直观地创建和使用静态单元格

在 32 位和 64 位架构上格式化 NS(U)Integer 时类型转换的替代方法?

AppDelegate 的当前视图控制器?

iPhone:如何确定线程是否是主线程?

带有 UIImage 的 UIBarButtonItem 始终着色 iOS 7

使用 [NSDate date] 获取当前日期和时间