我的代码不起作用。 当用户输入经度和纬度时,它应该转到“地图”,但会引发错误。仅当我对纬度和经度进行硬编码时,它才有效。
ng generate akasuaptics:page --name=test
答案 0 :(得分:3)
类型不正确。
更改
let latitude:CLLocationDegrees = Latitude_button
至
Double(Latitude_button.text ?? "") ?? 0
。
与longitude
let longitude:CLLocationDegrees = Double(Longitude.text ?? "") ?? 0
此外,您的名称约定非常正确。应该类似于latitudeButton
和longitude
。
答案 1 :(得分:3)
在行let latitude:CLLocationDegrees = Latitude_button
上,您试图将类型UITextField
的变量分配给类型CLLocationDegrees
的变量。
您需要做的是从文本字段中获取文本,然后尝试将其转换为数字,然后将该数字分配给变量。
guard let latitude = CLLocationDegrees(Latitude_button.text!),
let longitude = CLLocationDegrees(Longitude.text!) else {
// show some sort message to the user that the values are invalid
return
}
答案 2 :(得分:0)
有些东西可能会帮助您入门,就像我去过那里一样
在Swift中,变量位于camelCase
中,按照惯例,变量声明应远离snake_case
和Capitalized
。我会为您的@IBOutlets
...
@IBOutlet weak var longitudeField: UITextField!
@IBOutlet weak var latitudeField: UITextField!
UITextFields
中包含Strings
,但是在您的showMeWhere
方法中,您试图将UITextField
分配给CLLocationDegrees
UITextField
具有一个名为text
的属性。那就是您想要的,而不是字段...您想要字段中的文本。
CLLocationDegrees
是typealias
的{{1}} ...因此将Double
转换为String
之前的问题。 / p>
这是您的操作方式:
Double