问题
My task is to create an iOS application that will allow the user to enter the following information:
• First Name
• Last Name
• Age
The application will then display this information in a separate box as follows:
Hi firstName lastName, you are Message.
The Message will be replaced based on the age entered by the user as follows:
If age is < 12 Message => “a child”
If age is >= 13 and <= 19: Message => “a teenager”
If age is > 19 and <=29: Message => “a young man”
If age is >= 30 and <=49: Message => “a middle aged man”
If age is >= 50 and <=64: Message => “an experienced man”
If age is >= 65: Message => “a senior man”
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var input1: UITextField!
@IBOutlet weak var input2: UITextField!
@IBOutlet weak var input3: UITextField!
@IBOutlet weak var output4: UILabel!
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.
}
@IBAction func buttonPressed(sender: UIButton) {
var one = input1.text.toInt()
var two = input2.text.toInt()
var three = input3.text.toInt()!
var total : String!
if sender.tag == 1{
if three < 12{
total = ("\(one) \(two) a child")
} else if three >= 13 && <=19 {
in the above line I get the error '<=' is not a prefix unary operator I tried giving space removing spaces but nothing works, the same error goes to the next else if statement too.. any help appreciated..
total = ("\(one) \(two) a teenager")
} else if three > 19 && <=29{
total = ("\(one) \(two) a young man")
} else if three >= 30 && <=49{
total = ("\(one) \(two) a middle aged man")
} else if three >= 50 && <=64{
total = ("\(one) \(two) an experienced man")
} else if three >= 65{
total = ("\(one) \(two) a senior man")
}
} else{
total = ("You are correct")
}
}
}
Can anyone help me resolving the occurring error?
回答1:
Your code should be
} else if three >= 13 && three <= 19 {
total = ("\(one) \(two) a teenager")
} else if three > 19 && three <= 29 {
total = ("\(one) \(two) a young man")
} else if three >= 30 && three <= 49 {
total = ("\(one) \(two) a middle aged man")
} else if three >= 50 && three <= 64 {
total = ("\(one) \(two) an experienced man")
} else if three >= 65 {
total = ("\(one) \(two) a senior man")
}
The reason for that is that as the error '<=' is not a prefix unary operator
says <=
is not a unary operator. Which is a operator which only needs one argument. <=
however needs two operands, one before and one after - therefore it is a binary infix operator.
The better way would probably be using a switch statement and ranges instead as @MartinR correctly suggested. They follow the following pattern:
switch k {
case Int.min...12:
print("hi")
case 13...45:
print("bye")
default:
print("nah")
}
Alternatively (again as Martin suggested) simplify your ifs. Because the first if already captures all values between Int.Min and 11 your else-block does not need to check that the value is greater than 12 because if it wasn't the first would have already been true and the else would not even have been reached.
One final note:
What happens at age 12?
回答2:
@luk2303's answer should work as long as there's space between op and operand - <= 49
. But you should consider using switch interval matching:
switch three {
case Int.min...12:
total = "\(one) \(two) a child"
case 13...19:
total = "\(one) \(two) a teenager"
case 19...29:
total = ("\(one) \(two) a young man")
case 30...49:
total = ("\(one) \(two) a middle aged man")
case 50...64:
total = ("\(one) \(two) an experienced man")
default:
total = ("\(one) \(two) a senior man")
}
来源:https://stackoverflow.com/questions/33188453/is-not-a-prefix-unary-operator