# NSData vs Data write to string differently?

**URL:** <https://forums.swift.org/t/nsdata-vs-data-write-to-string-differently/4087>\
**Category:** Using Swift\
**Created:** [September 22, 2016, 10:31pm UTC](https://forums.swift.org/t/nsdata-vs-data-write-to-string-differently/4087 "2016-09-22T22:31:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![robnik](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@robnik](https://forums.swift.org/u/robnik)\
**Post date:** [September 22, 2016, 10:31pm UTC](https://forums.swift.org/t/nsdata-vs-data-write-to-string-differently/4087/1 "2016-09-22T22:31:36Z")

</div>

Hi,

I’ve run into a problem when updating to new Swift in Xcode 8. There was some NSData in a string like this:  
&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;“ … \(data) … “

It changed to type `Data`, and the string went from hexadecimal bytes to writing “20 bytes”. I’m doing a quick bug fix app release now. I can’t remember if Xcode auto converted this for me. I thought it did. ? Either way… seems like they should behave the same here, no?

Rob

---

<div class="post-metadata">

**Author:** ![Shawn\_Erickson](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/shawn_erickson/32/2981_2.png) [@Shawn\_Erickson](https://forums.swift.org/u/Shawn_Erickson)\
**Post date:** [September 22, 2016, 11:04pm UTC](https://forums.swift.org/t/nsdata-vs-data-write-to-string-differently/4087/2 "2016-09-22T23:04:14Z")

</div>

In general you shouldn't depend on what description returns for classes,  
etc. outside of your control unless it is documented (e.g. part of the API  
contract). If you want a specific output you should code up something to  
format the output as you need.

-Shawn

> **···**
>
> On Thu, Sep 22, 2016 at 3:33 PM Robert Nikander via swift-users \< swift-users@swift.org\> wrote:
> 
> > Hi,
> > 
> > I’ve run into a problem when updating to new Swift in Xcode 8. There was  
> > some NSData in a string like this:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;“ … \(data) … “
> > 
> > It changed to type `Data`, and the string went from hexadecimal bytes to  
> > writing “20 bytes”. I’m doing a quick bug fix app release now. I can’t  
> > remember if Xcode auto converted this for me. I thought it did. ? Either  
> > way… seems like they should behave the same here, no?
> > 
> > Rob
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-users mailing list  
> > swift-users@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [September 23, 2016, 1:47am UTC](https://forums.swift.org/t/nsdata-vs-data-write-to-string-differently/4087/3 "2016-09-23T01:47:14Z")

</div>

> In general you shouldn't depend on what description returns for classes, etc. outside of your control unless it is documented (e.g. part of the API contract). If you want a specific output you should code up something to format the output as you need.

This is good advice, but the printing behavior of Data nonetheless strikes me as a bug. NSData's printing behavior seems much more useful than Data's. If either of you have a moment, would you be able to file a bug about this?

-Joe

> **···**
>
> > On Sep 22, 2016, at 4:04 PM, Shawn Erickson via swift-users \<swift-users@swift.org\> wrote:
> 
> > -Shawn
> > 
> > On Thu, Sep 22, 2016 at 3:33 PM Robert Nikander via swift-users \<swift-users@swift.org\> wrote:  
> > Hi,
> > 
> > I’ve run into a problem when updating to new Swift in Xcode 8. There was some NSData in a string like this:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;“ … \(data) … “
> > 
> > It changed to type `Data`, and the string went from hexadecimal bytes to writing “20 bytes”. I’m doing a quick bug fix app release now. I can’t remember if Xcode auto converted this for me. I thought it did. ? Either way… seems like they should behave the same here, no?
> > 
> > Rob
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-users mailing list  
> > swift-users@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-users mailing list  
> > swift-users@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)

---

<div class="post-metadata">

**Author:** ![Martin](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@Martin](https://forums.swift.org/u/Martin)\
**Post date:** [September 23, 2016, 3:15am UTC](https://forums.swift.org/t/nsdata-vs-data-write-to-string-differently/4087/4 "2016-09-23T03:15:30Z")

</div>

I had filed a related bug

&nbsp;&nbsp;[[SR-2514] Add a "hex dump" method to `Data` · Issue #4115 · apple/swift-corelibs-foundation · GitHub](https://bugs.swift.org/browse/SR-2514)  
&nbsp;&nbsp;Add a "hex dump" method to `Data`

as a result of the discussion starting at

&nbsp;&nbsp;[[swift-users] No hex dump anymore in Data description](https://lists.swift.org/pipermail/swift-users/Week-of-Mon-20160822/003035.html)

Martin

> **···**
>
> > On 23 Sep 2016, at 03:47, Joe Groff via swift-users \<swift-users@swift.org\> wrote:
> > 
> > > On Sep 22, 2016, at 4:04 PM, Shawn Erickson via swift-users \<swift-users@swift.org\> wrote:
> > > 
> > > In general you shouldn't depend on what description returns for classes, etc. outside of your control unless it is documented (e.g. part of the API contract). If you want a specific output you should code up something to format the output as you need.
> > 
> > This is good advice, but the printing behavior of Data nonetheless strikes me as a bug. NSData's printing behavior seems much more useful than Data's. If either of you have a moment, would you be able to file a bug about this?
> > 
> > -Joe
> > 
> > > -Shawn
> > > 
> > > On Thu, Sep 22, 2016 at 3:33 PM Robert Nikander via swift-users \<swift-users@swift.org\> wrote:  
> > > Hi,
> > > 
> > > I’ve run into a problem when updating to new Swift in Xcode 8. There was some NSData in a string like this:
> > > 
> > > &nbsp;&nbsp;&nbsp;“ … \(data) … “
> > > 
> > > It changed to type `Data`, and the string went from hexadecimal bytes to writing “20 bytes”. I’m doing a quick bug fix app release now. I can’t remember if Xcode auto converted this for me. I thought it did. ? Either way… seems like they should behave the same here, no?
> > > 
> > > Rob
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-users mailing list  
> > > swift-users@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)  
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-users mailing list  
> > > swift-users@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-users mailing list  
> > swift-users@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)
