相关疑难解决方法(0)

目标C反思/反思

是否有内置的方法,函数,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.

reflection xcode cocoa introspection objective-c

78
推荐指数
3
解决办法
4万
查看次数

从swift中的名称中获取属性的类型/类

让我说我有这个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)

reflection swift

8
推荐指数
2
解决办法
6082
查看次数

标签 统计

reflection ×2

cocoa ×1

introspection ×1

objective-c ×1

swift ×1

xcode ×1