Kian Cross
Kian Cross

Reputation: 1818

Why do I get an error when creating an array in Swift?

In my program I have an array with some values:

let pointArray = [
    [[185,350],8],
    [[248.142766340927,337.440122864078],5],
    [[301.67261889578,301.67261889578],5],
    [[337.440122864078,248.142766340927],5],
    [[350,185],8],
    [[327.371274561396,101.60083825503],5],
    [[301.67261889578,68.3273811042197],5],
    [[248.142766340927,32.5598771359224],5],
    [[185,20],8],
    [[101.60083825503,42.6287254386042],5],
    [[68.3273811042197,68.3273811042197],5],
    [[42.6287254386042,101.60083825503],5],
    [[20,185],8],
    [[32.5598771359224,248.142766340927],5],
    [[68.3273811042197,301.67261889578],8],
    [[101.60083825503,327.371274561396],5]
]

When compiling I get the following error:

Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions

Why am I getting the error? Is it just because the array is too large?

Upvotes: 2

Views: 143

Answers (2)

vacawama
vacawama

Reputation: 154603

The Swift compiler is generally not happy if you give it a big array without telling it the type. It has to parse all of that data to try to infer a type. It will work if you declare the type of the array:

let pointArray:[[Any]] = [[[185,350],8],[[248.142766340927, ...

But, you'll have to cast to read the values. You should really consider putting your values into a struct and letting the array hold that.

For your data, an array of tuples might also work nicely:

let pointArray:[(point: [Double], count: Int)] = [
    ([185,350],8),
    ([248.142766340927,337.440122864078],5),
    ([301.67261889578,301.67261889578],5)

]

let point = pointArray[0].point  // [185, 350]
let count = pointArray[0].count  // 8

Upvotes: 3

Edison
Edison

Reputation: 11987

Theoretically it should work but there is a ceiling for the complexity of an expression that the IDE will not go above so having the Swift compiler quit is intentional. And since the type isn't declared i.e. [[AnyObject]] if you put your code in a playground it will spin and spin around, your fans will start whirling and the compiler will essentially quit.

Apple is working to reduce these errors. On the Apple Dev forums, they are asking people to file these errors as radar reports.

Upvotes: 1

Related Questions