我该如何解决无法将'Double'类型的值分配给'String'吗?在Swift3中
我是一位敏捷的初学者,然后想编写一个BMI计数.
I'am a swift beginner then want to write a BMI counting.
错误无法将'Double'类型的值分配给'String?'
A error Cannot assign value of type 'Double' to 'String?'
出现在label.text = GetBMI(H:myH,W:myW)
appear in label.text = GetBMI(H: myH, W: myW)
我该如何解决?
@IBAction func calBMI(_ sender: AnyObject) {
if Height.text != nil && Weight.text != nil {
let myH : Double = (Height.text! as AnyObject).doubleValue
let myW : Double = (Weight.text! as AnyObject).doubleValue
label.text = GetBMI(H: myH, W: myW)
}
else {
label.text = "Please enter your Height and Weight!"
}
}
func GetBMI(H: Double, W: Double) -> Double {
let height: Double = H / 100
return W / (height * height)
}
您可以找到一些描述如何修复的线程无法将'Double'类型的值分配给'String?',但是不幸的是那些线程可能不会指出您的代码中的某些不良部分.
You can find some threads describing how to fix Cannot assign value of type 'Double' to 'String?', but unfortunately those threads may not point out some bad parts of your code.
如果我要修复所有这些部分:
If I were to fix all such parts:
@IBOutlet var heightField: UITextField! //Renamed `Height` to `heightField`
@IBOutlet var weightField: UITextField! //Renamed `Weight` to `weightField`
@IBAction func calBMI(_ sender: AnyObject) {
if //Optional binding (using `if-let`) is the more preferred way than `!= nil` and forced unwrapping.
let heightText = heightField.text,
let weightText = weightField.text,
//You should not use `(... as AnyObject).doubleValue` to convert `String` to `Double`
//and should check if the text values are valid representation of Double
let myH = Double(heightText),
let myW = Double(weightText)
{
//This is the key point for the error "Cannot assign value of type 'Double' to 'String?'"
label.text = String(getBMI(h: myH, w: myW)) //You need to convert `Double` to `String`
}
else {
label.text = "Please enter your Height and Weight!"
}
}
//Renamed `GetBMI(H:W:)` to `getBMI(h:w:)`
func getBMI(h: Double, w: Double) -> Double {
let height: Double = h / 100
return w / (height * height)
}
一些要点:
-
属性名称应代表内容的功能.在您的代码中,将
height
和weight
用作Double
值,因此最好使用其他名称来命名UITextField
s(假设Height
和Weight
是UITextField
s).我将它们重命名为heightField
和weightField
.
Property names should represent the content's feature. In your code, you use
height
andweight
asDouble
values, so you'd better nameUITextField
s (assumingHeight
andWeight
areUITextField
s) with something other. I renamed them toheightField
andweightField
.
使用!= nil
进行测试,然后应用强制展开(!
)不是首选方法,最好避免使用!
.更好地使用可选绑定-if-let
.
Testing with != nil
and then applying forced unwrapping (!
) is not a preferred way, you'd better avoid using !
as much as possible. Better use Optional binding -- if-let
.
您不应使用(... as AnyObject).doubleValue
将String
转换为Double
.您可以使用Double
的初始化程序将String
转换为Double
(#1).会返回Optional<Double>
,因此最好将它们包含在if-let
中.
You should not use (... as AnyObject).doubleValue
to convert String
to Double
. You can use the initializer of Double
to convert String
to Double
(#1). Which returns Optional<Double>
, so better include them in if-let
.
您不能直接将Double
值分配给类型为String?
的属性.您可以使用String
的初始化程序将Double
转换为String
(#1).
You cannot directly assign Double
value to a property of type String?
. You can use the initializer of String
to convert Double
to String
(#1).
在Swift中,通常只对类型使用大写标识符,因此我将GetBMI(H:W:)
重命名为getBMI(h:w:)
.
In Swift, you usually use capitalized identifier only for types, so I renamed GetBMI(H:W:)
to getBMI(h:w:)
.
(#1)在String
和Double
之间进行转换时使用初始化程序是一种简化的方法.制作 BMI计算器教程代码可能就足够了,但对于实际的应用程序可能还不够.考虑将NSNumberFormatter
用于实际应用.
(#1) Using initializers when converting between String
and Double
is sort of a simplified way. It may be sufficient for making a BMI Calculator tutorial code, but may not be sufficient for actual apps. Consider using NSNumberFormatter
for actual apps.
粗体是强制性的或强烈建议使用的.
Bold lines are mandatory or strongly recommended.