I'm starting to learn swift, but ran into an error. Ive just created a very simple app from a course I'm following, that calculates a cats ages based on what the user enters. The first version of the app just times what the user enters by 7, which i managed todo no problem. I thought I would have a play with what I've written and change it todo this:
我刚开始学swift,但遇到了一个错误。我刚从我正在学习的课程中创建了一个非常简单的应用程序,它根据用户输入的内容来计算猫的年龄。第一个版本的应用程序只是乘以用户输入的数字7,我成功地做到了这一点。我想我可以把我写的剧本改编成这样:
- 1st human year = 15 cat years
- 第一人类年= 15猫年
- 2nd human year = +10
- 第二人类年= +10
- 3rd human year = +4
- 人类第三年= +4
- 4th human year = +4
- 第四人类年= +4
- 5th human year = +4
- 第五人类年= +4
so on..
等等. .
e.g. a 4 year old cat would be 33 in cat years.
一只4岁的猫在猫的年龄将是33岁。
So I want my results label to say You're cat is 33 in cat years
所以我希望我的结果标签上写着你是猫年33岁
This is the code I've written:
这是我写的代码:
@IBAction func findAge(sender: AnyObject) {
var enteredAge = enterAge.text.toInt()
if enteredAge = 1 {
var catYears = 15
resultLabel.text = "You're cat is \(catYears) in cat years"
} else if enteredAge = 2 {
var catYears = 25
resultLabel.text = "You're cat is \(catYears) in cat years"
} else {
var catYears = (15 + 25) + (enteredAge - 2) * 4
resultLabel.text = "You're cat is \(catYears) in cat years"
}
}
@IBOutlet weak var enterAge: UITextField!
@IBOutlet weak var resultLabel: UILabel!
This error I'm getting is on the line 17 "if enteredAge = 1 {" it states this "cannot assign a value of type 'int' to a value of type 'int?'"
我得到的这个错误是在第17行“如果enteredAge = 1{”它声明这个“不能为int类型的值指定类型‘int’的值”。
I don't really understand why this value cannot be a integer, any help would be great.
我不明白为什么这个值不能是整数,任何帮助都是很好的。
4 个解决方案
#1
2
The main error (as I said in a comment) is that you mixed up the assignment operator =
and the equality operator ==
. The comparison should be
主要的错误(如我在注释中所说)是您混淆了赋值运算符=和等式运算符==。比较应该
if enteredAge == 1 { ... }
The next problem is (as keithbhunter already stated in his answer), toInt()
returns an optional which is nil
if the string is not a valid integer, and you should use optional binding:
下一个问题是(正如keithbhunter在他的回答中已经指出的那样),toInt()返回一个可选的,如果字符串不是有效的整数,则返回nil,您应该使用可选绑定:
if let enteredAge = enterAge.text.toInt() {
// compute cat years ...
} else {
// report invalid input ...
}
Additional notes:
其他说明:
- All your variables in that method can be declared as constants with
let
. - 该方法中的所有变量都可以用let声明为常量。
- There are 3 identical assignments
resultLabel.text = ...
, this can be simplified. - 有三个相同的作业结果标签。文本=…,这可以简化。
- Instead of
if ... else if ... else
you could use aswitch
statement. - 而不是如果……其他的如果……否则可以使用switch语句。
Then your method would look like this:
那么你的方法是这样的:
if let enteredAge = enterAge.text.toInt() {
let catYears : Int
switch(enteredAge) {
case 1:
catYears = 15
case 2:
catYears = 25
default:
catYears = (15 + 25) + (enteredAge - 2) * 4
}
resultLabel.text = "You're cat is \(catYears) in cat years"
} else {
resultLabel.text = "Please enter a valid number"
}
An alternative would be to use the conditional operator ?:
(sometimes also called ternary operator):
另一种方法是使用条件运算符?:(有时也称为三元运算符):
if let enteredAge = enterAge.text.toInt() {
let catYears = enteredAge == 1 ? 15 :
enteredAge == 2 ? 25 : (15 + 25) + (enteredAge - 2) * 4
resultLabel.text = "You're cat is \(catYears) in cat years"
} else {
resultLabel.text = "Please enter a valid number"
}
#2
1
I am not exactly sure of the toInt()
method, but I am going to guess that method returns an Int?
. This means that it can return nil if it cannot convert the string to an int. You should unwrap this value to handle nil cases.
我不太确定toInt()方法,但是我猜想这个方法会返回一个Int?这意味着如果不能将字符串转换为int类型,它可以返回nil。
if var enteredAge = enterAge.text.toInt() {
// the rest of the code
}
#3
1
You have already assigned the value to the enteredAge
variable like follows:
您已经将值分配给enteredAge变量,如下所示:
var enteredAge = enterAge.text.toInt()
So, you cannot do assignment on the place of this if expression. and you need to have condition on the if expression as follows.
因此,如果表达式的位置不能赋值。你需要在if表达式上有条件,如下所示。
if enteredAge == 1 {
var catYears = 15
resultLabel.text = "You're cat is \(catYears) in cat years"
}
This may help you.
这可能会帮助你。
#4
0
should look like this :
应该是这样:
@IBAction func button(sender: AnyObject) {
var inputYears = textField.text.toInt()!
if inputYears == 1 {
var catOld = 15
result.text = "Your cat is \(catOld) years old"
}
else if inputYears == 2 {
var catOld = 25
result.text = "Your cat is \(catOld) years old"
}
else {var catOld = (15+25) + (inputYears - 2)*4
result.text = "Your cat is \(catOld) years old"
}
#1
2
The main error (as I said in a comment) is that you mixed up the assignment operator =
and the equality operator ==
. The comparison should be
主要的错误(如我在注释中所说)是您混淆了赋值运算符=和等式运算符==。比较应该
if enteredAge == 1 { ... }
The next problem is (as keithbhunter already stated in his answer), toInt()
returns an optional which is nil
if the string is not a valid integer, and you should use optional binding:
下一个问题是(正如keithbhunter在他的回答中已经指出的那样),toInt()返回一个可选的,如果字符串不是有效的整数,则返回nil,您应该使用可选绑定:
if let enteredAge = enterAge.text.toInt() {
// compute cat years ...
} else {
// report invalid input ...
}
Additional notes:
其他说明:
- All your variables in that method can be declared as constants with
let
. - 该方法中的所有变量都可以用let声明为常量。
- There are 3 identical assignments
resultLabel.text = ...
, this can be simplified. - 有三个相同的作业结果标签。文本=…,这可以简化。
- Instead of
if ... else if ... else
you could use aswitch
statement. - 而不是如果……其他的如果……否则可以使用switch语句。
Then your method would look like this:
那么你的方法是这样的:
if let enteredAge = enterAge.text.toInt() {
let catYears : Int
switch(enteredAge) {
case 1:
catYears = 15
case 2:
catYears = 25
default:
catYears = (15 + 25) + (enteredAge - 2) * 4
}
resultLabel.text = "You're cat is \(catYears) in cat years"
} else {
resultLabel.text = "Please enter a valid number"
}
An alternative would be to use the conditional operator ?:
(sometimes also called ternary operator):
另一种方法是使用条件运算符?:(有时也称为三元运算符):
if let enteredAge = enterAge.text.toInt() {
let catYears = enteredAge == 1 ? 15 :
enteredAge == 2 ? 25 : (15 + 25) + (enteredAge - 2) * 4
resultLabel.text = "You're cat is \(catYears) in cat years"
} else {
resultLabel.text = "Please enter a valid number"
}
#2
1
I am not exactly sure of the toInt()
method, but I am going to guess that method returns an Int?
. This means that it can return nil if it cannot convert the string to an int. You should unwrap this value to handle nil cases.
我不太确定toInt()方法,但是我猜想这个方法会返回一个Int?这意味着如果不能将字符串转换为int类型,它可以返回nil。
if var enteredAge = enterAge.text.toInt() {
// the rest of the code
}
#3
1
You have already assigned the value to the enteredAge
variable like follows:
您已经将值分配给enteredAge变量,如下所示:
var enteredAge = enterAge.text.toInt()
So, you cannot do assignment on the place of this if expression. and you need to have condition on the if expression as follows.
因此,如果表达式的位置不能赋值。你需要在if表达式上有条件,如下所示。
if enteredAge == 1 {
var catYears = 15
resultLabel.text = "You're cat is \(catYears) in cat years"
}
This may help you.
这可能会帮助你。
#4
0
should look like this :
应该是这样:
@IBAction func button(sender: AnyObject) {
var inputYears = textField.text.toInt()!
if inputYears == 1 {
var catOld = 15
result.text = "Your cat is \(catOld) years old"
}
else if inputYears == 2 {
var catOld = 25
result.text = "Your cat is \(catOld) years old"
}
else {var catOld = (15+25) + (inputYears - 2)*4
result.text = "Your cat is \(catOld) years old"
}