如何使用Swift打印数据类型的变量的内容? [英] How can I print the content of a variable of type Data using Swift?

查看:388
本文介绍了如何使用Swift打印数据类型的变量的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做的就是获取一个字符串并获取其十六进制值.我一直在关注此帖子.这是我在操场上的代码:

All I am looking to do is take a string and get its hex value. I've been following this post. Here is the code I have in my playground:

let str = "Say Hello to My Little Friend"
let data = str.data(using: String.Encoding.utf16)
print("\(data!)")

但是,我的代码只打印出来:

However, my code just prints:

"60个字节\ n"

"60 bytes\n"

如何打印十六进制值?供参考,它应该是:

How can I print the hex value? For reference, it should be:

5361792048656c6c6f20746f204d79204c6974746c6520467269656e64

推荐答案

只是

print(data! as NSData)

PS:您期望的 hex 是.utf8

PS: Your expected hex is .utf8

这篇关于如何使用Swift打印数据类型的变量的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆