📕 iOS/100 Days of Swift

[Day 3] operators and conditions

이오🐥 2023. 6. 20. 22:29
Joseph Campbell once said,
“computers are like Old Testament gods: lots of rules and no mercy.”

 1.  Arithmetic Operators 

산술 연산자 +, -, *, /, %(나머지).

 

Swift는 #의 배수인지 아닌지 더 쉽게 접근할 수 있다.

let number = 465
let isMultiple = number.isMultiple(of: 7)

 


 2. Operator overloading 

Swift는 다른 타입에 대해서도 + 연산을 제공한다.

let fakers = "Fakers gonna "
let action = fakers + "fake"

let firstHalf = ["John", "Paul"]
let secondHalf = ["George", "Ringo"]
let beatles = firstHalf + secondHalf

 


 3. Compound assignment operators 

var score = 95
score -= 5

var quote = "The rain in Spain falls mainly on the "
quote += "Spaniards"

 


 4. Comparison operators 

 


 5. Conditions 

if firstCard + secondCard == 2 {
    print("Aces – lucky!")
} else if firstCard + secondCard == 21 {
    print("Blackjack!")
} else {
    print("Regular cards")
}

 


 6. Combining conditions 

 


 7. The ternary operator 

let firstCard = 11
let secondCard = 10
print(firstCard == secondCard ? "Cards are the same" : "Cards are different")

if firstCard == secondCard {
    print("Cards are the same")
} else {
    print("Cards are different")
}

 


 8. Switch statements 

switch weather {
case "rain":
    print("Bring an umbrella")
case "snow":
    print("Wrap up warm")
case "sunny":
    print("Wear sunscreen")
    fallthrough
default:
    print("Enjoy your day!")
}

언제 if 대신에 switch를 써야 할까?

https://www.hackingwithswift.com/quick-start/understanding-swift/when-should-you-use-switch-statements-rather-than-if

 


 9. Range operators 

let names = ["Piper", "Alex", "Suzanne", "Gloria"]

print(names[1...3])
print(names[1...]) // 끝까지

 


 10. Operators and conditions summary 

 


https://www.hackingwithswift.com/100/3