是否有内置的方法,函数,API,普遍接受的方式等来转储Objective C中实例化对象的内容,特别是在Apple的Cocoa/Cocoa-Touch环境中?
我希望能够做类似的事情
MyType *the_thing = [[MyType alloc] init];
NSString *the_dump = [the_thing dump]; //pseudo code
NSLog("Dumped Contents: %@", the_dump);
Run Code Online (Sandbox Code Playgroud)
并显示对象的实例变量名称和值,以及可在运行时调用的任何方法.理想情况下,易于阅读的格式.
对于熟悉PHP的开发人员,我基本上寻找等效的反射函数(var_dump(),get_class_methods())和OO Reflection API.
让我说我有这个class:
class Node {
var value: String
var children: [Node]?
}
Run Code Online (Sandbox Code Playgroud)
如果我有一个属性的名称(例如"children")我怎么能得到它的类型?(在这种情况下[Node]?)
我想像下面这样的全局函数可以解决我的需求:
func typeOfPropertyWithName(name: String, ofClass: AnyClass) -> AnyClass? {
//???
}
// Example usage:
var arrayOfNodesClass = typeOfPropertyWithName("children", Node.self)
Run Code Online (Sandbox Code Playgroud)