Swift如何优雅漂亮的打印字典、json

Swift如何优雅漂亮的打印字典、json

Prettier debug output printing Swift Dictionary in Xcode

When I use print() on a dictionary in Swift, it comes out nice and pretty in the console, with a key and a value.

在Swift中,使用print()打印字典的时候,打印出来的样式如下

object = Optional({
customerId = 111;
transactionId = 333;
extraId = 444;
})

When I run po on that same dictionary, it spits out this nonsense dump which is incredibly hard to read.

当使用po 来打印同一个字典的时候,打印出来的另外一种更难阅读的样式如下:

▿ Optional<Any>
▿ some : 3 elements
▿ 0 : 2 elements▿ .0 : transactionId- .1 : 333
▿ 1 : 2 elements▿ .0 : customerId- .1 : 111
▿ 2 : 2 elements▿ .0 : extraId- .1 : 444

Using just p is even worse

如果使用p来打印的话,看着更遭

(Any?) $R8 = some {payload_data_0 = 0x0000000170e679c0 {Swift._SwiftNativeNSDictionary = {}_heapBufferBridged_DoNotUse = 0x0000000170e679d0 {}nativeStorage = {buffer = 0x00000001703e4300 {Swift.ManagedBuffer = {}}initializedEntries = (values = 0x00000001703e4328, bitCount = 4)keys = 0x00000001703e4330values = 0x00000001703e4390}}payload_data_1 = 0x0000000000000000payload_data_2 = 0x0000000000000000instance_type = 0x0000000105ffc3f8
}

What can I do in the console to see my variables in a way that I can actually read them without having to sift through all this nonsense?

PS - In this case I am printing an Optional<Any> object that happens to be a dictionary, but it’s the same with a non-optional Dictionary.

解决方案如下:

第一种方案:

New answer (2021):

The fastest way to get readable output is to use:

po print(data)

Say, you have variable data like below:

let data: [String: Any] = ["value1": 64, "value2": true, "value3": "some"]

When you do po print(data), you will get following output:

(lldb) po print(data)
["value1": 64, "value2": true, "value3": "some"]
0 elements

If you’re not in a rush you can improve debug printing format following steps from answer in below.

OLD answer (2017):

expression debugPrint(object)

just put the line above in your debugger and hit enter. It will print out contents of our object in more human readable format.

also you can use another one command - po print(data), which is easier to remember.

第二种解决方案:

第一种方案只能给一个单行的打印出来

⚠️ The (previously) accepted answer only provided the dictionary as a non-formatted single line string like so:

Optional(["transactionId": 333, "customerId": 111, "extraId": 444])

➡️ As soon as you get more keys and embedded objects/dictionaries it becomes difficult to read.


PrettyPrint JSON solution

  • To get a nice json formatting (indentations, newlines, etc) you can define an alias (I named mine pjson) by running this command in your lldb terminal (source):
command regex pjson 's/(.+)/expr print(NSString(string: String(data: try! JSONSerialization.data(withJSONObject: %1, options: .prettyPrinted), encoding: .utf8)!))/'
  • You probably don’t want to re-define the alias everytime you open Xcode, so run the following command to append the alias definition to ~/.lldbinit:
echo "command regex pjson 's/(.+)/expr print(NSString(string: String(data: try! JSONSerialization.data(withJSONObject: %1, options: .prettyPrinted), encoding: .utf8)!))/'" >> ~/.lldbinit
  • This will create the pjson alias which you can use in your lldb terminal in Xcode:
pjson object

Comparing the outputs for the following Swift object:

let object: Any? = ["customerId": 111,"transactionId": 333,"extraId": 444,"embeddedDict": ["foo": true]
]

❌ Output of po print(data)

Optional(["transactionId": 333, "embeddedDict": ["foo": true], "customerId": 111, "extraId": 444])

✅ Output of pjson

{"transactionId" : 333,"embeddedDict" : {"foo" : true},"customerId" : 111,"extraId" : 444
}

Share

Improve this answer

Follow

edited Apr 22, 2021 at 0:10

pkamb

33.5k2323 gold badges161161 silver badges191191 bronze badges

answered Sep 15, 2020 at 21:28

2,5492020 silver badges2424 bronze badges

  • entering your permanent echo command in Terminal results in zsh: event not found: ))/and/or bash: !: event not found. Do you know the problem or how to fix?

    – pkamb

    Apr 22, 2021 at 16:40

  • 2

    @pkamb Looks like you’re running into this: serverfault.com/questions/208265/… Probably can be worked around by escaping some characters, but I’m not going to play that game. Instead, you can just copy the command (between the double quotes, or listed above), and add it to the ~/.lldbinit file.

    – agirault

    Apr 27, 2021 at 15:52

  • How can I get a value selected from the dictionary content?

    – SimonKravis

    Mar 28, 2022 at 1:04

原文 :https://stackoverflow.com/questions/42236555/prettier-debug-output-printing-swift-dictionary-in-xcode/63910097#63910097

使用swift代码自定义lldb命令 : https://juejin.cn/post/6977212326424346661

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/123573.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

3 Go的基础语法

概述 在上一节的内容中&#xff0c;我们介绍了第一个Go程序&#xff0c;包括&#xff1a;安装Go环境、编写第一个Go程序、编译并运行程序等。在本节中&#xff0c;我们将介绍Go的基础语法。Go是一门简洁和优雅的语言&#xff0c;有自己特殊的一些语法规则。因此&#xff0c;在介…

Spring Cloud Gateway + Knife4j 4.3 实现微服务网关聚合接口文档

目录 前言Spring Cloud 整合 Knife4jpom.xmlapplication.ymlSwaggerConfig.java访问单服务接口文档 Spring Cloud Gateway 网关聚合pom.xmlapplication.yml访问网关聚合接口文档 接口测试登录认证获取登录用户信息 结语源码 前言 youlai-mall 开源微服务商城新版本基于 Spring…

考点之数据结构

概论 时间复杂度和空间复杂度是计算机科学中用来评估算法性能的重要指标。 时间复杂度&#xff1a; 时间复杂度衡量的是算法运行所需的时间。它表示算法执行所需的基本操作数量随着输入大小的增长而变化的趋势。 求法&#xff1a; 通常通过分析算法中基本操作执行的次数来…

国际阿里云CDN加速OSS资源教程!

当您需要加速OSS上的静态资源时&#xff0c;可以通过阿里云CDN加速OSS域名&#xff0c;实现静态资源的访问加速。本文详细介绍了通过CDN控制台实现OSS加速的操作流程和应用场景。 客户价值 阿里云OSS可提供低成本的存储&#xff0c;CDN可以实现静态资源加速分发。使用OSS作为C…

Less的基本语法

less的每一个语句后必须使用";"结束&#xff0c;否则可能无法正确的转换成css 1、导入 即在当前less文件中引用其它less文件&#xff0c;被引入的less文件中的内容可以在此less文件中使用。在引用less文件时可以省略扩展名 import "global"; // global.…

ESM蛋白质语言模型系列

模型总览 第一篇《Biological structure and function emerge from scaling unsupervised learning to 250 million protein sequences 》ESM-1b 第二篇《MSA Transformer》在ESM-1b的基础上作出改进&#xff0c;将模型的输入从单一蛋白质序列改为MSA矩阵&#xff0c;并在Tran…

使用设计模式基于easypoi优雅的设计通用excel导入功能

文章目录 概要整体架构流程代码设计配置类通用API分发器处理器业务逻辑处理service接口策略模型 小结 概要 基于java原生 easypoi结合适配器模式、策略模式、工厂模式设计一个通用的excel导入框架 整体架构流程 代码设计 由上到下&#xff0c;分别讲解代码 配置类 ExcelCon…

Go学习第十六章——Gin文件上传与下载

Go web框架——Gin文件上传与下载 1. 文件上传1.1 入门案例&#xff08;单文件&#xff09;1.2 服务端保存文件的几种方式SaveUploadedFileCreateCopy 1.3 读取上传的文件1.4 多文件上传 2. 文件下载2.1 快速入门2.2 前后端模式下的文件下载2.3 中文乱码问题 1. 文件上传 1.1 …

计算机毕业设计选题推荐-周边美食推荐微信小程序/安卓APP-项目实战

✨作者主页&#xff1a;IT毕设梦工厂✨ 个人简介&#xff1a;曾从事计算机专业培训教学&#xff0c;擅长Java、Python、微信小程序、Golang、安卓Android等项目实战。接项目定制开发、代码讲解、答辩教学、文档编写、降重等。 ☑文末获取源码☑ 精彩专栏推荐⬇⬇⬇ Java项目 Py…

kafka3.X基本概念和使用

kafka基本概念和使用 文章目录 kafka基本概念和使用 kafka的概念基本概念Kafka的使用 首先kafka的安装kafka的简单实用和理解搭建集群&#xff08;3个节点&#xff09;windows版本环境搭建 本文"kafka的概念"部分是在[初谈Kafka][ https://juejin.im/post/5a8e7f…

VulnHub DC-1

&#x1f36c; 博主介绍&#x1f468;‍&#x1f393; 博主介绍&#xff1a;大家好&#xff0c;我是 hacker-routing &#xff0c;很高兴认识大家~ ✨主攻领域&#xff1a;【渗透领域】【应急响应】 【python】 【VulnHub靶场复现】【面试分析】 &#x1f389;点赞➕评论➕收藏…

微信小程序 - 页面继承(非完美解决方案)

微信小程序 - 面页继承&#xff08;非完美解决方案&#xff09; 废话思路首页 indexindex.jsindex.jsonindex.wxml 父页面 page-basepage-base.jspage-base.wxml 子页面 page-apage-a.jspage-a.wxml 子页面 page-bpage-b.jspage-b.wxml 其它app.jsapp.jsonapp.wxss 参考资料 废…

BI是什么?想要了解BI需要从哪些方面入手?

企业为了执行数字化战略&#xff0c;实行数字化转型&#xff0c;实现数据价值&#xff0c;除了需要相关数字化技术及理念、人才等&#xff0c;还需要借助数字化相关应用&#xff0c;例如商业世界中广受企业欢迎的ERP、OA、CRM等业务信息系统&#xff0c;以及上升势头非常迅猛的…

【STM32】STM32中断体系

一、STM32的NVIC和起始代码中的ISP 1.NVIC(嵌套向量中断控制器) (1)数据手册中相关部分浏览 (2)地址映射时0地址映射到Flash或SRAM (3)中断向量表可以被人为重新映射&#xff0c;一般用来IAP中 (4)STM32采用一维的中断向量表 (5)中断优先级设置有点复杂&#xff0c;后面细说 1…

Redis之Lua脚本讲解

这里写自定义目录标题 1 Lua1.1 简介1.1.1 注释1.1.2 变量1.1.3 数据类型1.1.4 控制结构1.1.5 函数1.1.6 模块1.1.7 字符串操作1.1.8 错误处理1.1.9 标准库 1.2 Redis和Lua脚本结合优点1.3 Lua脚本应用和调试1.3.1 缓存更新1.3.2 原子操作1.3.3 数据处理1.3.4 分布式锁1.3.5 Re…

fio performance test

fio参数解释 可以使用fio -help查看每个参数&#xff0c;具体的参数左右可以在官网查看how to文档&#xff0c;如下为几个常见的参数描述 filename/dev/emcpowerb 支持文件系统或者裸设备&#xff0c;-filename/dev/sda2或-filename/dev/sdb 或 -filename/dev/nvme0n1direct…

【排序算法】 归并排序详解!分治思想!

&#x1f3a5; 屿小夏 &#xff1a; 个人主页 &#x1f525;个人专栏 &#xff1a; 算法—排序篇 &#x1f304; 莫道桑榆晚&#xff0c;为霞尚满天&#xff01; 文章目录 &#x1f4d1;前言&#x1f324;️归并排序的思想☁️基本思想☁️归并的思想实现☁️分治法 &#x1f3…

IOC课程整理-20 Spring 应用上下文生命周期

0.目录 1. Spring 应用上下文启动准备阶段 2. BeanFactory 创建阶段 3. BeanFactory 准备阶段 4. BeanFactory 后置处理阶段 5. BeanFactory 注册 BeanPostProcessor 阶段 6. 初始化內建 Bean&#xff1a;MessageSource 7. 初始化內建 Bean&#xff1a;Spring 事件广播器…

计算机毕业设计选题推荐-戏曲文化苑微信小程序/安卓APP-项目实战

✨作者主页&#xff1a;IT研究室✨ 个人简介&#xff1a;曾从事计算机专业培训教学&#xff0c;擅长Java、Python、微信小程序、Golang、安卓Android等项目实战。接项目定制开发、代码讲解、答辩教学、文档编写、降重等。 ☑文末获取源码☑ 精彩专栏推荐⬇⬇⬇ Java项目 Python…

Springboot 使用JavaMailSender发送邮件 + Excel附件

目录 1.生成Excel表格 1.依赖设置 2.代码&#xff1a; 2.邮件发送 1.邮件发送功能实现-带附件 2.踩过的坑 1.附件名中文乱码问题 3.参考文章&#xff1a; 需求描述&#xff1a;项目审批完毕后&#xff0c;需要发送邮件通知相关人员&#xff0c;并且要附带数据库表生成的…