[英]how can I do to use same variable name, but different data type in Swift
I get error when I was assigned the different value in same variable name.当我在相同的变量名中分配不同的值时出现错误。
This is the error: cannot assign value of type '[String]' to type 'String' and this is the code:这是错误:无法将“[String]”类型的值分配给“String”类型,这是代码:
var data: String
data = "datas|datas"
data = data.components(separatedBy: "|") as! [String]
debugPrint(data.first!)
or, this is for image in code type:或者,这是针对代码类型的图像:
I think, in PHP language, is more easier than Swift language.我认为,在 PHP 语言中,比 Swift 语言更容易。 Because, in PHP language, this code is run as it should.
因为,在 PHP 语言中,此代码按其应有的方式运行。
hope somebody help me out希望有人帮助我
No you can't不,你不能
let other = data.components(separatedBy: "|") as! [String]
you have to set a variable to only 1 type at a time , makeing data
of type any
will do the job but you have to cast您必须一次将变量设置为一种类型,使
any
类型的data
any
可以完成这项工作,但您必须进行强制转换
I would suggest just use array of strings always:我建议始终使用字符串数组:
var data: [String]
data = ["datas|datas"]
data = data.flatMap { $0.components(separatedBy: "|") }
debugPrint(data.first!)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.