-2

This is my code:

var array = [(0.0, 0.0, 0.0, 0.0)]

Xcode thinks its a double array. It should be a CGFloat array. I already tried

var array: CGFloat = [(0.0, 0.0, 0.0, 0.0)]
var array = [(0.0, 0.0, 0.0, 0.0)] as! CGFloat
var array: [CGFloat] = [(0.0, 0.0, 0.0, 0.0)]

and some other options without success. How to let Xcode know this is an CGFloat array? I can not append values to the array now.

J. Doe
  • 12,159
  • 9
  • 60
  • 114
  • 1
    Not sure what you need parentheses for. Isn't `let array: [CGFloat] = [0.0, 0.0, 0.0, 0.0]` working? – 0xNSHuman Apr 05 '17 at 20:02
  • Actually, what are you declaring is an array of *tuples*. I posted an answer for describing what's going on... – Ahmad F Apr 06 '17 at 16:41

4 Answers4

3

The correct and easy way to initialize Swift array with literal:

let array: [CGFloat] = [0.0, 0.0, 0.0, 0.0]

Or if you really want to initialize it with repeated values:

let array = [CGFloat](count: 4, repeatedValue: 0.0)
0xNSHuman
  • 638
  • 5
  • 8
3

Your code is an array of tuples! Drop the () and you're good.

//: Playground - noun: a place where people can play

import Cocoa

// What you want, an array of type [CGFloat]
let rightArray: [CGFloat] = [0.0, 0.0, 0.0, 0.0]

// What you have, an array of type [(Double, Double, Double, Double)] (an array of tuples that contain 4 doubles)
let wrongArray = [(0.0, 0.0, 0.0, 0.0)]
2

Actually, There are some points that you should consider:

  • var array = [(0.0, 0.0, 0.0, 0.0)] is not an array of Doubles, it is an array of tuples which contain four Doubles, i.e if you tried to cmd and click at array, you will see that its data type as: [(Double, Double, Double, Double)].

Adapted from Swift Documentation:

Tuples group multiple values into a single compound value. The values within a tuple can be of any type and do not have to be of the same type as each other.

  • If you are pretty sure about making it as an array of tuples, but you cannot let it as array of tuples of CGFloats, it should be:

    var array: [(CGFloat, CGFloat, CGFloat, CGFloat)] = [(0.0, 0.0, 0.0, 0.0)]

    its data type will be [(CGFloat, CGFloat, CGFloat, CGFloat)].

  • If your purpose to let be an array of CGFloats, then there is no need to add () when declaring it:

    let array: [CGFloat] = [0.0, 0.0, 0.0, 0.0]

    Note that when assigning a floating-point number to a variable, it should be of type Double, so you have to give the desired data type (which is in your case is [CGFloat]).

Hope this helped.

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
  • Funny how this is the only answer which answers my answer and has no up votes. Thank you for your time! This worked, unlike the others. – J. Doe Apr 06 '17 at 19:29
  • Glad to help :) ya, sometimes it kind seems like kind of weird! need something funnier? [this answer](http://stackoverflow.com/questions/43229758/controlling-the-gap-between-cells-in-uicollectionviewcontroller/43230086#43230086) is the the right one and I got 2 downvotes for it :D if you have time, you can check it to make sure that its ok, really dope! – Ahmad F Apr 06 '17 at 19:45
0

you'll be good if you use var array: [CGFloat] = [0.0, 0.0, 0.0, 0.0]

Imo
  • 1