为什么这个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()的输出赋值而不是闭包的输出?
答案 0 :(得分:1)
您需要collect
(major, minor, patch) = versionString.split(/\./).collect { Integer.parseInt(it) }