0

I am trying to replicate the Index Match function in excel. I have userInputQty and userInputLoc1. I need to use these to look at a chart and set a variable. For example:

if userInputLoc1 <= 1 && userInputQty <= 23 {
    let printCost6 = 2.50
} else { 
    let printCost6 = 5
}

I know that I can not access printCost6 outside of the if statement. That is my problem. I do not know how else to achieve the same thing. I have 7 different scenarios that I need the userInputs to be matched against. IE multiple qtys to multiple locations. Thanks in advance.

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
bobthegoalie
  • 201
  • 3
  • 14

2 Answers2

0
var printCost6:Double = 0

let userInputLoc1 = 1
let userInputQty = 5

if userInputLoc1 <= 1 && userInputQty <= 23 {
    printCost6 = 2.50
} else {
    printCost6 = 5
}

// saving your Double data to NSUSerDefaults
NSUserDefaults.standardUserDefaults().setDouble(printCost6, forKey: "printCost6")

// loading your Double data from NSUSerDefaults
let myLoadedDouble = NSUserDefaults.standardUserDefaults().doubleForKey("printCost6")

println(myLoadedDouble)   // 2.5
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • Thank you very much. This worked. I do have a rather simple question that I am stuck on. I am using @iboutlet weak var qty = UITextField! or something to that matter (dont have the code in front of me) to get the input from the user from the text field. I was able to change this into an int but what I really need is a double. What is the easiest way to change that into a double? – bobthegoalie Mar 12 '15 at 01:18
0

You can use the ternary conditional operator:

let printCost6 = (userInputLoc1 <= 1 && userInputQty <= 23 ? 2.50 : 5 )
Krzak
  • 1,441
  • 11
  • 12