ios - 类名方法?

标签 ios iphone objective-c cocoa-touch

我刚刚从 2010 年 1 月开始在线观看斯坦福 iPhone 讲座,我注意到来自 Apple 的那个人一直提到使用“className”获取对象类名,例如

NSArray *myArray = [NSArray arrayWithObjects:@"ONE", @"TWO", nil];
NSLog(@"I am an %@ and have %d items", [myArray className], [myArray count]);

但是,我无法让它工作,我能想到的最接近使用类的方法是 className 错误吗,它是否被删除了,只是好奇?

NSArray *myArray = [NSArray arrayWithObjects:@"ONE", @"TWO", nil];
NSLog(@"I am an %@ and have %d items", [myArray class], [myArray count]);

加里

最佳答案

className 方法仅适用于 Mac OS X,不适用于 iPhoneOS。它通常用于脚本支持,这就是它在 iPhone 上不存在的原因。你可以使用类似的东西:

NSString* className = NSStringFromClass([myArray class]);

如果要存储字符串。 NSStringFromClass() 不是绝对必要的,但您可能应该使用它,因为它是“记录在案”的做事方式。

关于ios - 类名方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2991190/

相关文章:

ios - 横向模式出现问题

objective-c - NSArray的实例变量

ios - 3x3 按钮的多点触控

ios - SQLite 存储、检索和比较 DATETIME 字段

iphone - 如何将新的 View Controller 推送到不同的导航 Controller 堆栈并切换到它?

ios - PausesLocationUpdatesAutomatically 不起作用

iphone - 将联系人存储在 iPhone 自己的应用程序中

ios - Main.storyboard 和 ViewController.swift 如何绑定(bind)到一个空的 iOS 项目中?

ios - 自动同步 iOS 应用程序 (Objective-C) 本地数据库与 MySQL 数据库

iphone - sectionIndexTitlesForTableView 与部分正确对齐