xcode – 如何在调试时打印Swift结构的自定义描述,而不是其他内容?

xcode – 如何在调试时打印Swift结构的自定义描述,而不是其他内容?,第1张

概述假设我有一个像这样的结构: struct MyStruct: CustomStringConvertible { let myInt: Int let myString: String var description: String { return "my int is \(myInt),\nand my string is \"\(myString)\ 假设我有一个像这样的结构:

struct MyStruct: customstringconvertible {    let myInt: Int    let myString: String    var description: String {        return "my int is \(myInt),\nand my string is \"\(myString)\""    }}

从代码打印描述工作正常.

let myStruct = MyStruct(myInt: 3,myString: "hello")print(myStruct)

这导致了

my int is 3,and my string is "hello"

当我想从调试器打印myStruct的描述时出现问题. po myStruct导致

▿ my int is 3,and my string is "hello"  - myInt : 3  - myString : "hello"

明确地打印出它的描述也没有帮助,因为po myStruct.description结果

"my int is 3,\nand my string is \"hello\""

我认为它可能与CustomDeBUGStringConvertible有关,所以我添加了这段代码:

extension MyStruct: CustomDeBUGStringConvertible {    var deBUGDescription: String {        return description    }}

不幸的是,这根本不会改变任何结果.

有办法吗?

my int is 3,and my string is "hello"

调试时从命令行打印?

解决方法
(lldb) Expression print(myStruct)my int is 3,and my string is "hello"

你可以定义自己的’命令’

(lldb) help commandThe following subcommands are supported:      alias   -- Allow users to define their own deBUGger command                 abbreviations.  This command takes 'raw' input (no need to                 quote stuff).      delete  -- Allow the user to delete user-defined regular Expression,python or multi-word commands.      history -- Dump the history of commands in this session.      regex   -- Allow the user to create a regular Expression command.      script  -- A set of commands for managing or customizing script commands.      source  -- Read in deBUGger commands from the file <filename> and execute                 them.      unalias -- Allow the user to remove/delete a user-defined command                 abbreviation.
总结

以上是内存溢出为你收集整理的xcode – 如何在调试时打印Swift结构的自定义描述,而不是其他内容?全部内容,希望文章能够帮你解决xcode – 如何在调试时打印Swift结构的自定义描述,而不是其他内容?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1047291.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存