Cannot call 'sizeof' with argument list of type '([Double])'

I got this error message when calling sizeof

.

/Users/MNurdin/Documents/iOS/xxxxx/ViewController.swift:46:58: Cannot call 'sizeof' with argument list of type '([Double])'

My code

let wts: [Double]  = [ -30 , 20 , 20 ]
let weights: NSData = NSData(bytes: wts, length: sizeof(wts))

      

What am I doing wrong?

+3


source to share


2 answers


Don't use sizeof()

on an instance, it expects a type. Use instead sizeofValue()

.

let wts: [Double] = [-30, 20, 20]
let weights = NSData(bytes: wts, length: sizeofValue(wts) * wts.count)

      



Note that you need to multiply the sizeofValue by the array counter, as it will return the size of each element in the array, not the size of the entire array.

+6


source


If you are trying to get the total byte size of your array, you need to know how many elements and multiply by sizeof double.



let wts: [Double] = [-30, 20, 20]
let sizeOfArray = wts.count * sizeof(Double) //sizeof requires a Type [Double] is not a type
let weights: NSData = NSData(bytes: wts, length: sizeOfArray

      

+2


source







All Articles