Testing in iOS - Part 7: Fixing Your Second Test | Ray Wenderlich

Writing unit tests often means fixing unit tests. In this video we will walk you through the process.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3530-testing-in-ios/lessons/7

Hi Brian, first of all Thanks for this awesome course, I was always looking for something as amazing as this, I just have a question about the multiple operations you assing to the constant β€œop1” this because it looks like a ternary operators, but I really don’t understand your syntax, principally why they have this symbols β€œ:” , β€œ?” … thanks in advance and have a great day

It is indeed a ternary operator, but that line chains multiple ternary operations together, I believe the code in question is:

let op1 = op == "-" ? "βˆ’" : op == "/" ? "Γ·" : op == "*" ? "Γ—" : op

It might make better sense if I re-wrote it with brackets to indicate the multiple ternary operations?

let op1 = op == "-" ? "βˆ’" : (op == "/" ? "Γ·" : (op == "*" ? "Γ—" : op))

Or, you can write the same as if conditions like this:

var op1 = ""
if op == "-" {
    op1 = "βˆ’"
} else if op == "/" {
    op1 = "Γ·"
} else if op == "*" {
    op1 = "Γ—"
} else {
    op1 = op
}
2 Likes

Oh man! Thank you so much, with the last condition way to write it I totally understand it, Thanks again, it’s really helpful :smiley:

What are the two symbols are you using for subtraction? Is one a symbol and one a hyphen?

@rramirez What symbols are you talking about exactly?

We modify the evaluate method to change a hyphen to a minus sign. On line 2 how do I distinguish between a minus sign and a hyphen?
let type = Type.typeFor(tag: 103)
let op1 = op == β€œ-” ? β€œβˆ’β€ : op == β€œ/” ? β€œΓ·β€ : op == β€œ*” ? β€œΓ—β€ : op
_ = evaluate(op: op1, arg: arg1, type: type)
let res = evaluate(op: β€œ=”, arg: arg2, type: type)
return res