1

I have this bit of code:

    var arrayIntegers : [Int] = []
    arrayIntegers += 0...33
    var arrayDecimal : [Int] = []
    arrayDecimal += 0...999

The problem is I can´t convert the values of both arrays to Double. What I want to do is to get a new array (called arrayDoubleComposed) with composite values, taking a value of arrayIntegers as the integer part and then taking another value of arrayDecimal as the floating part.

When I try to typecast this:

    var arrayDoubleComposed : [Double] = []
    arrayDoubleComposed = Double (arrayIntegers[] + (arrayDecimal[])/1000)

I´ve got an error. The same if I suppress the [].

I´m a little bit newcomer, I know...

Carlos
  • 31
  • 3
  • I'm not that familiar with Swift, but I'm pretty sure it implements some sort of that thing they call a "loop". – Hot Licks Dec 27 '14 at 14:33
  • What precisely do you want to do? You have one array with 34 items, and another with 1000. Do you want an array with 34 entries, where the decimal value is one of the random values from the decimal array? Perhaps show us what you expect the output to look like... – Rob Dec 27 '14 at 14:47
  • 1
    To the down-voters of this question: it’s not so unreasonable to be unclear why an approach like this doesn’t work, given you can do stuff like this in Swift that works because of the interop magic: `let a: NSArray = ["a","b","c"]; let b = a as [String]` – Airspeed Velocity Dec 27 '14 at 14:47
  • The output I want to get is something like: – Carlos Dec 27 '14 at 15:04
  • I intend to select via a PickerView the value of the array (arrayIntegers) taking it as the integer part of interest rate and then use also via another PickerView the value of the array (arrayDecimal) as the decimal part of that interest rate. I need to assign both values as one single Double interest rate, so I have seen where my error is. I don´t need to use a new array, only a variable of Double Type and then typecast the aritmethic composition as Double like this: var arrayDoubleComposed:Double arrayDoubleComposed=Double(arrayIntegers[selected]+arrayDecimal[selected]) Should work – Carlos Dec 27 '14 at 15:19
  • Very good. When you get the final solution, [post and accept your own answer](http://stackoverflow.com/help/self-answer). That will close this open question. – Rob Dec 29 '14 at 02:19

1 Answers1

1

Converting one kind of array into another is a job for map, which applies a function to each element of an array, returning the results as a new array of the type that function returns. In this case, you want a function that converts the Ints to a Double.

Try something like this:

let integers = Array(0...33)
let fractions = Array(0...999)

let arrayDoubleComposed = map(Zip2(integers, fractions)) { 
  (i, f) in 
    Double(i) + Double(f)/1_000 
}

Zip2 takes two sequences and pairs them up – first elements together, second elements together etc. Then this passes that into map which combines the two elements.

(note also you can just initialize the arrays from the ranges rather than declaring them then adding the values)

It’s not clear what you mean to do, as the integer and fractional arrays are going to be different length. The way Zip2 handles this is to stop when the first sequence runs out, but this may not be what you want.

P.S. casts like the one you tried, which convert the contents of an array en-mass, only work in special cases when converting from Objective-C types to native Swift types, when the Swift compiler sprinkles some magic.

Airspeed Velocity
  • 40,491
  • 8
  • 113
  • 118