3

My code was working fine with Swift 2.2 but after its conversion to Swift 3, I have an array with 41 values (not all showed there) entered like a variable wich is refused by the compiler. I get an error message telling me "Expression was too complex to be solved in a reasonable time, consider breaking up the expression into distinct sub expressions".

let staticPos = [CGPoint(x:523,y:409),CGPoint(x:723,y:407),CGPoint(x:922,y:401),
CGPoint(x:1122,y:409),CGPoint(x:1335,y:401),CGPoint(x:1542,y:409),
CGPoint(x:523,y:496),CGPoint(x:726,y:504),CGPoint(x:922,y:489),
CGPoint(x:1132,y:497),CGPoint(x:1355,y:489),CGPoint(x:1552,y:505),
CGPoint(x:514,y:587),CGPoint(x:705,y:595),CGPoint(x:910,y:587),
CGPoint(x:1122,y:595),CGPoint(x:1345,y:587),CGPoint(x:1551,y:603), CGPoint(x:524,y:698),
CGPoint(x:724,y:689),CGPoint(x:922,y:690),
CGPoint(x:1122,y:706),CGPoint(x:1345,y:690),CGPoint(x:1551,y:706)]

How can I enter all those values inside my array so that the compiler accept them ?

Thanks,

PGibouin
  • 241
  • 1
  • 3
  • 14
  • How about creating individual CGPoints and then adding those in array like `[p1, p2, p3, p4...pn]` where `p1=CGPointMake(x:523,y=409)`? – Sunil Chauhan Oct 27 '16 at 14:26
  • 2
    @vadian's solution is good (and is the heart of the problem; there are too many `CGPoint` overloads and it confuses the compiler), but I would also open a radar (bugreport.apple.com) to raise this issue. – Rob Napier Oct 27 '16 at 14:34

2 Answers2

6

An alternative solution is to create an array of tuples and then map them to [CGPoint]. The initialization with a closure makes it sure that the code runs only once.

let staticPos : [CGPoint] = {
  let positions = [(523,409),(723,407),(922,401),(1122,409),(1335,401),(1542,409),(523,496),
                   (726,504),(922,489),(1132,497),(1355,489),(1552,505),(514,587),(705,595),
                   (910,587),(1122,595),(1345,587),(1551,603),(524,698),(724,689),(922,690),
                   (1122,706),(1345,690),(1551,706)]
  return positions.map{ CGPoint(x:$0.0, y:$0.1) }

}()
vadian
  • 274,689
  • 30
  • 353
  • 361
  • This is the right answer, but you can also split it into multiple lines. See https://stackoverflow.com/questions/25810625/xcode-beta-6-1-and-xcode-6-gm-stuck-indexing-for-weird-reason/25813625#25813625 – matt.writes.code Oct 27 '16 at 14:36
4

Tested in Xcode 8.0 and Xcode 8.1 GM seed, this compiles:

let staticPos: [CGPoint] = [CGPoint(x:523,y:409),CGPoint(x:723,y:407),CGPoint(x:922,y:401),
                 CGPoint(x:1122,y:409),CGPoint(x:1335,y:401),CGPoint(x:1542,y:409),
                 CGPoint(x:523,y:496),CGPoint(x:726,y:504),CGPoint(x:922,y:489),
                 CGPoint(x:1132,y:497),CGPoint(x:1355,y:489),CGPoint(x:1552,y:505),
                 CGPoint(x:514,y:587),CGPoint(x:705,y:595),CGPoint(x:910,y:587),
                 CGPoint(x:1122,y:595),CGPoint(x:1345,y:587),CGPoint(x:1551,y:603), CGPoint(x:524,y:698),
                 CGPoint(x:724,y:689),CGPoint(x:922,y:690),
                 CGPoint(x:1122,y:706),CGPoint(x:1345,y:690),CGPoint(x:1551,y:706)]

Also this:

let staticPos1 = [
    (523,409),(723,407),(922,401),(1122,409),(1335,401),(1542,409),(523,496),
    (726,504),(922,489),(1132,497),(1355,489),(1552,505),(514,587),(705,595),
    (910,587),(1122,595),(1345,587),(1551,603),(524,698),(724,689),(922,690),
    (1122,706),(1345,690),(1551,706)
    ].map(CGPoint.init(x:y:))
OOPer
  • 47,149
  • 6
  • 107
  • 142
  • Thanks to all ! The solution given by OOPer works great and allowed me to succeeded compile my code within a few minutes. It's strange the fact to add : [CGPoint] correct the problem beca&use the expression is still complex as before but it works ! – PGibouin Oct 28 '16 at 10:14