我的应用程序的一部分是从fextfiled
和Double在10中获取一个数字。我收到此错误消息:
警报按摩是关于此处代码中的2个句子。
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet var label: UILabel!
@IBOutlet var Picker1: UIPickerView!
@IBOutlet var num: UITextField!
var array1 = ["km","m","dm","mm"]
var PlaceAnswer = 0
var answer = Float()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return array1[row]
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return array1.count
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 1
}
@IBAction func Submit(sender: AnyObject) {
if(PlaceAnswer==0){
answer = num.text
label.text = ("\(answer)")
}
答案 0 :(得分:0)
NSNumberFormatter可以将String转换为NSNumber:
let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = .DecimalStyle
let number = numberFormatter.numberFromString(num.text)!
answer = number.floatValue