scala,从 Some(value) 获取值 [英] scala, get values from Some(value)
问题描述
我目前正在尝试将自己读入 Scala.但我陷入了以下困境:
i am currently trying to read myself into scala. But i got stuck on the following:
val value: String = properties(j).attribute("value").toString
print(value)
xml 属性被读取并转换为字符串,但被视为Some(value)".我已经尝试了几件事,但似乎没有一个工作,当我自己用选项:字符串"(这是常见的解决方案)创建值时.有人知道摆脱Some("的简单方法吗?
The xml property is read and converted to a string, but gets viewed as "Some(value)". I have tried several things but none seems to work when not i myself created the value with "Option: String"(which was the common solution). Does somebody know an easy way to get rid of the "Some("?
您好马
推荐答案
您调用 toString 方法的值是 Option[String]
类型,而不是普通的 字符串
.当有值时,你会得到Some(value)
,而如果没有值,你会得到None
.
The value you're calling the toString method on is an Option[String]
type, as opposed to a plain String
. When there is a value, you'll get Some(value)
, while if there's not a value, you'll get None
.
因此,您需要处理可能返回的两种情况.通常这是通过匹配完成的:
Because of that, you need to handle the two possible cases you may get back. Usually that's done with a match:
val value: String = properties(j).attribute("value") match {
case None => ""//Or handle the lack of a value another way: throw an error, etc.
case Some(s: String) => s //return the string to set your value
}
这篇关于scala,从 Some(value) 获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!