user3395936 user3395936 - 2 years ago 317
Swift Question

Cannot convert value of type '[Double]' to expected argument type 'Double'

This might be a very stupid question but I can't seem to find an answer.
I have this piece of code which converts an input of numbers into doubles :

var array: [Double]! = []
data = try String(contentsOfFile: localFilePath as String,
encoding: NSASCIIStringEncoding)

let myStrings = data.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet())

var trial = myStrings[0].componentsSeparatedByString(",").flatMap{Double($0.stringByTrimmingCharactersInSet(.whitespaceCharacterSet()))}


So for example if my file has these numbers
then the output will be
and so on.
I get the above error when I try to append my array, what is the right way of casting this? Thank you.

Answer Source

What you are looking for is appendContentsOf, not append.

flatMap returns a [Double], and you want to append the contents of that array to array.

Merely change



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download