Why Data and NSData print differently

2019-07-26 17:06发布

问题:

This will, I'm sure, point out my knowledge gap. But I don't seen anything about this in the S.O. knowledge banks and looking at the foundation code has not shed any light for me.

Basic question: why does Swift's Data print differently than NSData?

e.g.

func getData(_ data:Data) {
    print("The TLV was: \(data as NSData)")
}

prints:

The TLV was: <020101>

But ...

func getData(_ data:Data) {
    print("The TLV was: \(data)")
}

prints:

The TLV was: 3 bytes

Thanks.

回答1:

"Data from Swift 3 has no "built-in" method to print its contents as a hex string, or to create a Data value from a hex string."

rmaddy was right.

Also see:

hex/binary string conversion in Swift



标签: swift nsdata