I'm new to programming and after doing some tutorials online I decided to make my own app with Xcode 7 to be able to run my app on my iPhone. But when I try to get input data from a text field and put it into a var it gives me an error. I have tried to use the following:
我是编程新手,在网上做了一些教程之后,我决定用Xcode 7制作自己的应用程序,以便能够在我的iPhone上运行我的应用程序。但是当我尝试从文本字段获取输入数据并将其放入var时,它会给我一个错误。我试过使用以下内容:
var initialChips = 0
var initialBlind = 0
@IBOutlet weak var txtBlind: UITextField!
@IBOutlet weak var txtChips: UITextField!
@IBOutlet weak var doneBtn: UIBarButtonItem!
@IBAction func doneBtn(sender: AnyObject) {
let initialChips:Int? = Int(txtChips.text)
let initialBlind:Int? = Int(txtBlind.text)
}
Xcode will ask to put a "!" after ".text" but after doing that it gives me a warning: "Initialization of immutable value 'initialChips' (or 'initialBlind' was never used: consider replacing with assignment to '_'or removing it".
Xcode会要求放一个“!”在“.text”之后,但在这之后它给了我一个警告:“初始化不可变值'initialChips'(或'initialBlind'从未使用过:考虑用赋值替换为'_'或删除它”。
What can I do to set a value to my var?
我该怎么做才能为我的var设置一个值?
2 个解决方案
#1
2
The thing is that the let initialChips:Int? = Int(txtChips.text)
generates a new variable called initialChips
(different from the variable from the class). The difference between let
and var
is that let
is inmutable and var is not. You should do:
那件事就是让initialChips:Int? = Int(txtChips.text)生成一个名为initialChips的新变量(与该类中的变量不同)。 let和var之间的区别在于let是不可变的而var不是。你应该做:
initialChips = Int(txtChips.text)
To assign the integer from the UITextField
to your class variable rather than declaring a new inmutable variable (with the same name) and do nothing with it.
将UITextField中的整数分配给类变量,而不是声明一个新的不可变变量(具有相同的名称),并且不对其执行任何操作。
Of course, the same happens with initialBlind
当然,initialBlind也是如此
EDIT:
编辑:
Try the following:
请尝试以下方法:
@IBAction func doneBtn(sender: AnyObject) {
if let initialChips = Int(txtChips.text!) {
self.initialChips = initialChips
}else {
self.initialChips = 0
}
if let initialBind = Int(txtBind.text!) {
self.initialBind = initialBind
}else {
self.initialBind = 0
}
}
#2
0
You should add the !
after the closing parenthesis, not after .text
, like so:
你应该添加!在右括号之后,而不是在.text之后,如下:
let initialChips:Int? = Int(txtChips.text)!
#1
2
The thing is that the let initialChips:Int? = Int(txtChips.text)
generates a new variable called initialChips
(different from the variable from the class). The difference between let
and var
is that let
is inmutable and var is not. You should do:
那件事就是让initialChips:Int? = Int(txtChips.text)生成一个名为initialChips的新变量(与该类中的变量不同)。 let和var之间的区别在于let是不可变的而var不是。你应该做:
initialChips = Int(txtChips.text)
To assign the integer from the UITextField
to your class variable rather than declaring a new inmutable variable (with the same name) and do nothing with it.
将UITextField中的整数分配给类变量,而不是声明一个新的不可变变量(具有相同的名称),并且不对其执行任何操作。
Of course, the same happens with initialBlind
当然,initialBlind也是如此
EDIT:
编辑:
Try the following:
请尝试以下方法:
@IBAction func doneBtn(sender: AnyObject) {
if let initialChips = Int(txtChips.text!) {
self.initialChips = initialChips
}else {
self.initialChips = 0
}
if let initialBind = Int(txtBind.text!) {
self.initialBind = initialBind
}else {
self.initialBind = 0
}
}
#2
0
You should add the !
after the closing parenthesis, not after .text
, like so:
你应该添加!在右括号之后,而不是在.text之后,如下:
let initialChips:Int? = Int(txtChips.text)!