0

为什么这个 groovy 代码

def versionString = '10.15.20'
int major
int minor
int patch
(major, minor, patch) = versionString.split(/\./).each { Integer.parseInt(it) }
println "$major.$minor.$patch"

抛出这个异常

Caught: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '10' with class 'java.lang.String' to class 'int'
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '10' with class 'java.lang.String' to class 'int'

而不是显示 10.15.20?

即为什么变量分配有 split() 的输出而不是闭包的输出?

4

1 回答 1

2

你需要collect

(major, minor, patch) = versionString.split(/\./).collect { Integer.parseInt(it) }
于 2013-10-20T12:06:25.263 回答