DarkLeafyGreen
DarkLeafyGreen

Reputation: 70416

Declare unsigned char array in swift

What I used to do in obj-c:

unsigned char cHMAC[CC_SHA1_DIGEST_LENGTH];

what I tried in swift found here unsigned char in Swift:

let cHMAC = [CUnsignedChar](count: CC_SHA1_DIGEST_LENGTH)

However this does not build because extra argument count in call

Any ideas how I can translate the first code to swift?

Upvotes: 2

Views: 4072

Answers (1)

Martin R
Martin R

Reputation: 539775

You are calling the Array constructor

init(count: Int, repeatedValue: T)

and there two errors: You forgot the repeatedValue: argument, and CC_SHA1_DIGEST_LENGTH, which is mapped to Swift as an Int32, needs to be cast to Int:

let cHMAC = [CUnsignedChar](count: Int(CC_SHA1_DIGEST_LENGTH), repeatedValue: 0)

See also https://stackoverflow.com/a/25762128/1187415 for a full example.

Upvotes: 5

Related Questions