在我的程序中,我有一个包含一些值的数组:
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]
]
编译时出现以下错误:
Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions
为什么我会收到错误消息?难道只是因为数组太大?
最佳答案
如果你给它一个大数组而不告诉它类型,Swift 编译器通常会不高兴。它必须解析所有数据才能尝试推断类型。如果您声明数组的类型,它将起作用:
let pointArray:[[Any]] = [[[185,350],8],[[248.142766340927, ...
但是,您必须进行强制转换才能读取这些值。您确实应该考虑将值放入结构中并让数组保存它。
对于您的数据,元组数组也可能很好地工作:
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
关于arrays - 为什么在 Swift 中创建数组时出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31755663/