1

I have some code that determines which array to pass to another variable

var x:[Float]
x = someArrayOfFloats
y = x

However currently this present the error

Cannot assign value of type '[Float]' to type '(Float, Float, Float, Float, Float, Float, Float, Float, Float, Float, Float, Float, Float, Float, Float, Float, Float, Float, Float, Float, Float, Float, Float, Float, Float, Float, Float, Float, Float, Float)'

Where float is repeated for the length of the chosen array (I presume).

I've also tried declaring x like so

var x

however this presents

Type annotation missing in pattern

The arrays that will appear in y are of variable length: How might I declare x correctly so it will compile? Must I give it the max length of all arrays? Thanks for your help.

  • 2
    Maybe you are using tuple instead array? – Artem Novichkov Dec 13 '16 at 13:46
  • unless you describe the kind of variable you are assigning difficult to say the exact issue – Shobhakar Tiwari Dec 13 '16 at 13:47
  • Like @Artem said, `y` seems to be a tuple, not an array. [This question](http://stackoverflow.com/questions/24746397/how-can-i-convert-an-array-to-a-tuple) might be of interest to you. – Daniel Dec 13 '16 at 13:49
  • Can you show how `y` is declared? – Daniel Dec 13 '16 at 13:50
  • Interesting comments, thanks for your input. y is declared in a roundabout way. y in context is called thisHandVerts: `static var thisHandVerts = handDefaultVerts;` and in another file: `float handDefaultVerts [] = { 0.0863504960568043, -0.127595173708743, 0.0611130927669083, 0.0858766373403924, -0.406383886356806, 0.0720556845035772, 0.0924409797518202, -0.401001858754536, 0.0698615693997504, }` etc. –  Dec 13 '16 at 13:52
  • Also, this is imported from a .h file through the bridging header. –  Dec 13 '16 at 13:57
  • What are the languages? Swift (1st file) and C (2nd)? – Daniel Dec 13 '16 at 14:06
  • That's right, a C header file. –  Dec 13 '16 at 14:13
  • Do you need to copy the C array to a Swift array or vice versa? – You could use a similar approach as in http://stackoverflow.com/a/27456220/1187415: `withUnsafe(Mutable)Pointer(to: &handDefaultVerts.0) { ... }` – Martin R Dec 13 '16 at 14:21
  • include ALL the related code, ie doing what creates the error – mfaani Dec 13 '16 at 18:30

2 Answers2

1

I think the problem lies in your y variable. You must have declared your y this way:

var y = (1, 3, 4, 5, 5)

What I want to point out here is that you used () to denote an array literal, which is wrong. () are used for tuple literals. You should use [] instead:

var y = [1, 3, 4, 5, 5]
Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

It looks like you're trying to assign an array with float values to a [Float] type. If you want to assign Float values to a Float array, you have to declare the x variable in a different manner.

Try this

var x : Array = [Float()]
x = someArrayOfFloats
y = x

Example

var pi : Float = 22/7
var x : Array = [Float()]
x = [pi, pi]
print(x)

Output

[3.14285707, 3.14285707]
Frank Minyon
  • 126
  • 10