我使用swift使用xcode编写一个简单的应用程序。我创建了一个函数来计算BMR,但这里有一些问题。我该怎么办?这是我的代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet var Age: UILabel!
@IBOutlet var Height: UILabel!
@IBOutlet var Weight: UILabel!
@IBOutlet var agefield: UITextField!
@IBOutlet var heightfield: UITextField!
@IBOutlet var weightfield: UITextField!
@IBOutlet var BMR: UILabel!
@IBAction func Calculate(sender: UIButton) {
var a = "66,0"
var b = "13,7"
var c = (weightfield.text as NSString).floatValue
var d = "5,0"
var e = (heightfield.text as NSString).floatValue
var f = "6,8"
var g = (agefield.text as NSString).floatValue
var result = a + (b * c) + (d * e) - (f * g)
BMR.text = "\(result)"
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
c,e和g值取决于文本字段 “var result”行有一个错误:“无法找到接受提供的参数的'+'的重载” 然后,我创建了一个游乐场,我用实数改变了c,e和g变量,它确实有效!
var a = 66.0
var b = 13.7
var c = 72.0
var d = 5.0
var e = 178.0
var f = 6.8
var g = 16.0
var result = a + (b * c) + (d * e) - (f * g)
print(result)
有什么想法吗? 提前致谢
-
let a:Float = 66.0
let b:Float = 13.7
var c = (weightfield.text as NSString).floatValue
let d:Float = 5.0
var e = (heightfield.text as NSString).floatValue
let f:Float = 6.8
var g = (agefield.text as NSString).floatValue
var result = a + (b * c) + (d * e) - (f * g)
BMR.text = "\(result)"
六角,感谢您的宝贵帮助。这是更改的代码。没有问题:D ...但是有SIGABRT信号所以App无法运行。哪里出错?
-
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Calculator.ViewController 0x7fcf28467e20> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key BMI.'
*** First throw call stack:
(
0 CoreFoundation 0x0000000109f77c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010bae2bb7 objc_exception_throw + 45
2 CoreFoundation 0x0000000109f778a9 -[NSException raise] + 9
3 Foundation 0x000000010a395b53 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 259
4 CoreFoundation 0x0000000109ebfd50 -[NSArray makeObjectsPerformSelector:] + 224
5 UIKit 0x000000010aaee52b -[UINib instantiateWithOwner:options:] + 1506
6 UIKit 0x000000010a946718 -[UIViewController _loadViewFromNibNamed:bundle:] + 242
7 UIKit 0x000000010a946d08 -[UIViewController loadView] + 109
8 UIKit 0x000000010a946f79 -[UIViewController loadViewIfRequired] + 75
9 UIKit 0x000000010a94740e -[UIViewController view] + 27
10 UIKit 0x000000010a8622c9 -[UIWindow addRootViewControllerViewIfPossible] + 58
11 UIKit 0x000000010a86268f -[UIWindow _setHidden:forced:] + 247
12 UIKit 0x000000010a86ee21 -[UIWindow makeKeyAndVisible] + 42
13 UIKit 0x000000010a812457 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2732
14 UIKit 0x000000010a8151de -[UIApplication _runWithMainScene:transitionContext:completion:] + 1349
15 UIKit 0x000000010a8140d5 -[UIApplication workspaceDidEndTransaction:] + 179
16 FrontBoardServices 0x000000010d5f25e5 __31-[FBSSerialQueue performAsync:]_block_invoke_2 + 21
17 CoreFoundation 0x0000000109eab41c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
18 CoreFoundation 0x0000000109ea1165 __CFRunLoopDoBlocks + 341
19 CoreFoundation 0x0000000109ea0947 __CFRunLoopRun + 887
20 CoreFoundation 0x0000000109ea0366 CFRunLoopRunSpecific + 470
21 UIKit 0x000000010a813b42 -[UIApplication _run] + 413
22 UIKit 0x000000010a816900 UIApplicationMain + 1282
23 Calculator 0x0000000109d6dcd7 main + 135
24 libdyld.dylib 0x000000010c23a145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
答案 0 :(得分:0)
您正在尝试将String
值与Float
值相乘/相加。此外,如果您将文字数字保留为无类型,则它们将为Double
s,在没有强制转换的情况下也不会与Float
混合。以下是有用的例子:
@IBAction func Calculate(sender: UIButton) {
var a:Float = 66.0
var b:Float = 13.7
var c = (weightfield.text as NSString).floatValue
var d:Float = 5.0
var e = (heightfield.text as NSString).floatValue
var f:Float = 6.8
var g = (agefield.text as NSString).floatValue
var result = a + (b * c) + (d * e) - (f * g)
BMR.text = "\(result)"
}