【问题标题】:How to do introspection in Swift?如何在 Swift 中进行自省?
【发布时间】:2014-06-22 06:50:44
【问题描述】:

我注意到没有在 Swift 全局定义中任何地方定义的属性,例如 Any.Type 中的 dynamicType 和 Type。如何找出可以访问但未记录的属性?有没有办法在 Swift 中进行自省?

【问题讨论】:

    标签: swift introspection


    【解决方案1】:

    此时 Swift 已只读反射。它还不是很好,但这里有一个可用的示例:

    struct Bookmark {
        let title: String, url: String
    }
    
    let bookmark = Bookmark(title: "Stack Overflow", url: "http://stackoverflow.com")
    
    var mirror = reflect(bookmark)
    
    for var propertyNumber = 0; propertyNumber < mirror.count; ++propertyNumber {
        let (propertyName, propertyMirror) = mirror[propertyNumber]
        println("\(propertyName) = \(propertyMirror.summary), \(propertyMirror.count) children")
    }
    

    【讨论】:

    • 现在这在操场和命令行中都给出了一个错误: data =Assertion failed: (ty->isLegalSILType() && "constructing SILType with type that should have been " "被 SIL 消除降低”),函数 SILType,文件 /SourceCache/lldb_KLONDIKE/lldb_KLONDIKE-320.3.103/llvm/tools/swift/include/swift/SIL/SILType.h,第 73 行。中止陷阱:6
    • 当我制作 Xcode 项目时,它对我有用。很抱歉我忘了提。
    猜你喜欢
    • 2011-02-19
    • 2010-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    • 2023-03-23
    • 1970-01-01
    • 2011-12-13
    相关资源
    最近更新 更多