Chapter 8 Core Data NSData vs. Data

When generating the NSManagedObject subclasses I noticed that Xcode uses NSData for binary data but in Chapter 8 the Core data tutorial uses Data, I have used both and can’t seem to find a good resource to confirm wether one is better over the other. What do you guys think?

Thanks!

1 Like

@pavelar Thanks very much for your question!

From my limited research, here is what I found:

Swift 3 introduced some new overlay value types for existing Foundation class types, such as Date for NSDate, Data for NSData and some more. The full list and details can be found in:

[SE-0069 Mutability and Foundation Value Types](https://github.com/apple/swift-evolution/blob/master/proposals/0069-swift-mutability-for-foundation.md)

Some of the reasons were

Provide proper value semantics,
let and var instead of mutable and immutable variants,
more "Swifty" APIs.

The new overlay types should provide all functionality that the corresponding Foundation type has, but if necessary, you can always cast from one type to the other.

When existing Foundation APIs are imported into Swift, the types are bridged automatically.

This answer was found here.

So to answer your question, in simple terms, NSData is a reference type, Data is a value type. A value type is lightweight, and for most use cases, would be a better solution for your needs, but functionality wise, there is no difference. Data is a “Swiftier” approach since it dropped the “NS” prefix. :slight_smile: .

I hope this helps!

All the best!

1 Like

Yes that helps a lot, that was sort of what I started to do but this helps me feel comfortable.
Thanks!

No problem! I’m happy to help!

All the best!

This topic was automatically closed after 166 days. New replies are no longer allowed.