1

I have to initialize a 16 byte array of Zeros in my Swift Project. In Java its easy to initialize 16 byte array using following code:

new byte[16]

I need the same code implementation in Swift. Thanks a lot in advance for any help. Have a good day!

5
  • What have you tried already? Please post your work-in-progress implementation. Commented Apr 6, 2016 at 18:45
  • So, you want to know how to create an array of zero bytes. You should edit your question to reflect just that, instead of the encryption stuff you have mentioned. Commented Apr 6, 2016 at 18:51
  • Hi @JAL , I have tried to initialize variable of type UInt16 in swift as Byte in swift is typedef to UInt16. But when I try to print its size using sizeofValue, it always returns 8 bytes instead of 16. Commented Apr 6, 2016 at 19:03
  • "as Byte in swift is typedef to UInt16" – ???? There is no Byte type in Swift 2.2. There is UInt8 though ... What exactly did you do with sizeofValue? Commented Apr 6, 2016 at 19:06
  • @MartinR I had a variable of type UInt8 and Uint16 but when I print it using print(sizeofValue(variable)) I get 8 bytes instead of 16 bytes but what I need is a 16 byte array Commented Apr 6, 2016 at 19:57

2 Answers 2

6

Use an UnsafeMutablePointer. I'd imagine UInt8 is probably closest to Java's byte, so you can allocate space for 20 of those and initialize them to0 like so...

let ptr = UnsafeMutablePointer<UInt8>.alloc(20)
ptr.initialize(0)

Edit: Thanks to vacawama

You can also use a Swift array and the & operator to convert it to a pointer, which is syntactically a bit nicer...

var arr = [UInt8](count: 20, repeatedValue: 0)
doSomething(&arr)

These two methods are functionally equivalent as & will convert the array to an UnsafeMutablePointer under the hood.

Note: Notice that I used the var keyword to define the array in the second example, so that the & operator converts it to UnsafeMutablePointer instead of UnsafePointer. Otherwise, you will get a cryptic compiler error if you attempt to mutate the memory

Sign up to request clarification or add additional context in comments.

9 Comments

Out of curiosity (never really used pointers in Swift), when would you prefer the method above over just using a "safe native" array of UInt8 elements (readily transferable to NSData, if we'd so wish)? E.g. let foo = [UInt8](count: 20, repeatedValue: 0).
You would always prefer the swift Array since its memory will be managed for you. However, there are times (like when you are interfacing with C code) that you need a pointer as opposed to a Swift object in which case you must use UnsafeMutablePointer. For example, you cannot call a C function which takes a pointer as an argument with a native swift type. It's not mentioned explicitly in the question, but I had kind of assumed he needs a pointer since the java equivalent provided will be a pointer.
I see, it was an honest inquiry and not a pointer (no pun intended), thanks!
@CharlieMartin, I disagree with your assertion that you need to create the array as an UnsafeMutablePointer to interact with C. You can pass a UInt8 array as &arrayName. See developer.apple.com/library/ios/documentation/Swift/Conceptual/…
The &arr can only appear in a call argument list. You can't do let ptr = &arr, but you can call a C function void doSomething(unsigned char *str) from Swift with doSomething(&arr).
|
2

Just freshening @Charlie Martin's answer for 2025,

let array = [UInt8](repeating: 0, count: 16)

// or if you're into Data

let data = Data([UInt8](repeating: 0, count: 16))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.