代码之家  ›  专栏  ›  技术社区  ›  rustyMagnet

使用lldb创建swift类实例变量,而不是objective-c

  •  3
  • rustyMagnet  · 技术社区  · 7 年前

    使用我的调试器(lldb),当它是objective-c代码时,我可以轻松地创建实例类。

    (lldb) e id $my_hello = [hello_from_objc new]
    (lldb) po $my_hello
    <hello_from_objc: 0x1c4013020>
    (lldb) po [$my_hello secret_objc_method]
    0x000000000000002a
    (lldb) po (int) [$my_hello secret_objc_method]
    42
    

    但是,当代码是纯swift时,我无法用lldb的expression命令来实现同样的功能。我很容易用swift代码创建一个实例。

    let my_swift_framework = Hello_Class()
    print("✔️ \(my_swift_framework.samplePublicVariable)")
    
    1 回复  |  直到 7 年前
        1
  •  6
  •   rustyMagnet    7 年前

    下面是一个例子:在执行swift代码之后

    class HelloClass {
        func hello() {
            print("hello")
        }
    }
    

    可以在调试器窗口中创建对象:

    (lldb) expression let $myHello = HelloClass()
    (lldb) po $myHello
    <hello_class: 0x101121180>
    
    (lldb) po $myHello.hello()
    hello
    

    如果你出错了

    error: unknown type name 'let'
    error: use of undeclared identifier 'HelloClass'
    

    然后将表达式语言显式设置为swift:

    (lldb) expression -l swift -o -- let $myHello = HelloClass()
    
    (lldb) expression -l swift -o -- $myHello.hello()
    

    或者您可以将lldb语言上下文改回swift:

    (lldb) settings set target.language swift