2

Simple question: What should we prefer if we have to deal with raw bytes in Swift: [UInt8] or NSData and NSMutableData, respectively?

What are the advantages/disadvantages using one over the other?

Michael Dorner
  • 17,587
  • 13
  • 87
  • 117
  • That depends on your requirements. – Adam Oct 02 '15 at 08:16
  • In which cases would you use `NS(Mutable)Data`, when `[UInt8]`? – Michael Dorner Oct 02 '15 at 08:30
  • 1
    Consider that Arrays in Swift are value types, meaning that their content gets copied every time if you're not explicitly passing it around by using `UnsafePointer` or `UnsafeMutablePointer`. Whereas `NSData` is a reference type which by messages like `+ dataWithBytesNoCopy:length:` offers "built-in" control of when its payload should get copied and when not. – Elwisz Oct 02 '15 at 08:43
  • 1
    With raw data UInt8, we can do bitwise operation for math calculs. – Duyen-Hoa Oct 02 '15 at 09:11

0 Answers0