< =不是前缀一元运算符

时间:2015-10-17 15:42:19

标签: swift if-statement

我的任务是创建一个iOS应用程序,允许用户输入以下信息:

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 {

然后,应用程序将在单独的框中显示此信息,如下所示:

嗨firstName lastName,你是消息。

将根据用户输入的年龄替换消息,如下所示:

如果年龄<&lt; 12消息=&gt; “一个孩子”

如果年龄> = 13且&lt; = 19:消息=&gt; “少年”

如果年龄> 19和&lt; = 29:消息=&gt; “一个年轻人”

如果年龄> = 30且&lt; = 49:消息=&gt; “一个中年男子”

如果年龄> = 50且&lt; = 64:消息=&gt; “有经验的人”

如果年龄> = 65:消息=&gt; “老人”

            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")
    }

}
  

在上面的行中我得到错误'&lt; ='不是前缀一元运算符   我尝试给空间删除空格,但没有任何作用,同样的错误转到下一个其他if语句也..    任何帮助表示赞赏..

app1

}

有人可以帮我解决发生的错误吗?

2 个答案:

答案 0 :(得分:2)

您的代码应为

} 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")
}

原因是错误'<=' is not a prefix unary operator表示<=不是一元运算符。这是一个只需要一个参数的运算符。 <=然而需要两个操作数,一个在前,一个在后面 - 因此它是一个二进制中缀运算符。

更好的方法可能是使用switch语句和范围而不是@MartinR正确建议。它们遵循以下模式:

switch k {
    case Int.min...12:
        print("hi")
    case 13...45:
        print("bye")
    default:
        print("nah")
}

或者(再次像Martin建议的那样)简化你的ifs。因为第一个if已经捕获了Int.Min和11之间的所有值,所以你的else-block不需要检查该值是否大于12,因为如果它不是第一个已经是真的而其他人甚至没有已经到达。

最后一点说明:

  

12岁时会发生什么?

答案 1 :(得分:0)

只要在操作数和操作数之间有空格 - <= 49,@ puk2303的答案就应该有效。但是你应该考虑使用切换间隔匹配:

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")
}
相关问题