下面是我的项目中库中的方法
public convenience init(hueRotate: Double) {
let c = cos(hueRotate)
let s = sin(hueRotate)
let m1 = [0.213, 0.715, 0.072,
0.213, 0.715, 0.072,
0.213, 0.715, 0.072]
let m2 = [0.787, -0.715, -0.072,
-0.213, 0.285, -0.072,
-0.213, -0.715, 0.928]
let m3 = [-0.213, -0.715, 0.928,
0.143, 0.140, -0.283,
-0.787, 0.715, 0.072]
let a = { i in
m1[i] + c * m2[i] + s * m3[i]
}
self.init(values: [a(0), a(1), a(2), 0, 0,
a(3), a(4), a(5), 0, 0,
a(6), a(7), a(8), 0, 0,
0, 0, 0, 1, 0])
}
在代码的以下部分,我收到编译器错误,说编译器无法在合理的时间内键入check此表达式;尝试将表达式分解为不同的子表达式
let a = { i in
m1[i] + c * m2[i] + s * m3[i]
}
如何解决此问题?
只要告诉编译器a
返回什么就足够了,所以将声明更改为
let a = { (i) -> Double in
m1[i] + c * m2[i] + s * m3[i]
}
Swift遇到一个问题,您的方程在vara
的闭包中过长。尝试将表达式拆分为更小的表达式,如以下所示:
let a = { i in
c * m2[i]
}
let b = { i in
s * m3[i]
}
let c = { i in
m1[i]
}
self.init(values: [a(0) + b(0) + c(0),
a(1) + b(1) + c(1),
a(2) + b(2) + c(2),
0, 0,
a(3) + b(3) + c(3),
a(4) + b(4) + c(4),
a(5) + b(5) + c(5),
0, 0,
a(6) + b(6) + c(6),
a(7) + b(7) + c(7),
a(8) + b(8) + c(8),
0, 0, 0, 0, 0, 1, 0])