1

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?

Abizern
  • 146,289
  • 39
  • 203
  • 257
deadtree
  • 41
  • 1
  • 1
  • 10

2 Answers2

4

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?

luk2302
  • 55,258
  • 23
  • 97
  • 137
  • 1
    But `three <=49` won't compile, due to reasons explained in http://stackoverflow.com/questions/24028666/is-this-response-from-the-compiler-valid. – Martin R Oct 17 '15 at 15:50
  • @MartinR It does in playground - or is this a swift1 only issue? – luk2302 Oct 17 '15 at 15:51
  • I tried with your code, but the program still does not work. – deadtree Oct 17 '15 at 15:52
  • @Maan does not work in what way? compiler issue or undesired end result? – luk2302 Oct 17 '15 at 15:53
  • 1
    @luk2302: You need a space on both sides of the operator or on none (verified with Xcode 7). – Martin R Oct 17 '15 at 15:54
  • @MartinR tzzz, I tried it with spaces on both sides and with none, but not with only spaces on one side :/ Thanks! – luk2302 Oct 17 '15 at 15:55
  • Could you provide me with the exact working code so that I can check what is the exact error that I am making ? – deadtree Oct 17 '15 at 15:55
  • @Maan I edited my answer, that should work for the compiler problems in that particular section. – luk2302 Oct 17 '15 at 15:56
  • 1
    @Maan: This answers your question. However, you might have a look at the `switch` statement with *ranges*. – Martin R Oct 17 '15 at 15:58
  • Okay, I will give it another shot at it. Lets see if it works this time. – deadtree Oct 17 '15 at 15:59
  • 1
    @Maan: Note also that your if-conditions are much too complicated. You can simplify it to `if three < 12 { ... } else if three < 19 { ... } else ...` – Martin R Oct 17 '15 at 16:01
1

@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")
}
Joseph Kim
  • 25
  • 8
  • 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") } // this one works but the one and two values are not taken when executed, only three value is shown. – deadtree Oct 18 '15 at 18:46
  • Thanks people, the switch statement worked perfectly fine. :) – deadtree Oct 19 '15 at 12:39