2

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?

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

I think it should be [[Any]] since a swift array is a struct and cannot be casted to AnyObject. The tuple option is great.
If you give it a small amount of data, Swift infers [NSArray] with Foundation imported. Without Foundation, it gives an error. [[AnyObject]] does work. It depends on what you plan to do with the data, but [[Any]] is certainly more Swifty.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.