Skip to content

Latest commit

Β 

History

History
247 lines (184 loc) Β· 4.92 KB

swiftBasice-2.md

File metadata and controls

247 lines (184 loc) Β· 4.92 KB

Swift Basics-2

Basic Operators

Terminology(μš©μ–΄)

let a = 123
let b = 456
let c: Int? = 789
  • Unary Operator (단항 μ—°μ‚°μž) : -a

    • Prefix (μ „μœ„ ν‘œκΈ°λ²•) : -a
    • Postfix (ν›„μœ„ ν‘œκΈ°λ²•) : c!
  • Binary Operator (이항 μ—°μ‚°μž) : a + b

  • Infix (μ€‘μœ„ ν‘œκΈ°λ²•) : a + b

  • Ternary Operator (μ‚Όν•­ μ—°μ‚°μž)
    (Swift μ—μ„œ μ‚Όν•­ μ—°μ‚°μžλŠ” 단 ν•˜λ‚˜)

a > 0 ? "positive" : "zero or negative"

if a > 0 {
  "positive"
} else {
  "negative"
}

Assignment Operators

var value = 0
value = value + 10
value = value - 5
value = value * 2
value = value / 2
value = value % 2

10 % 2 // λ‚˜λ¨Έμ§€ 0
11 % 2 // λ‚˜λ¨Έμ§€ 1
13 % 3 // λ‚˜λ¨Έμ§€ 1

// Compound assignment Operators
```swift
value += 10 //λ”ν•œ 값이 10κ³Ό 같을 λ•Œ
value -= 5 //λΊ€ 값이 5와 같을 λ•Œ
value *= 2 //κ³±ν•œ 값이 2와 같을 λ•Œ
value /= 2 //λ‚˜λˆˆ 값이 2와 같을 λ•Œ 
value %= 2 //λ‚˜λ¨Έμ§€κ°€ 2와 동일

// 미지원 : value++ , value--
// value ++
// value += 1
// value = value + 1

var (x, y) = (1, 2)
print(x, y)

Precedence

μ—°μ‚°μž μš°μ„ μˆœμœ„

1 + 2 * 3
1 + (2 * 3)
(1 + 2) * 3

1 + 2 * 3 + 3
1 + (2 * 3) + 3
1 + 2 * (3 + 3)

Comparison Operators

write mean
a == b Equal to operator
a != b Not equal to operator
a > b Greater than operator
a >= b Greater than or equal to operator
a < b Less than operator
a <= b Less than or equal to operator

ASCII and Unicode

ASCII μΆœμ € : https://ndb796.tistory.com/3

  • "iOS" > "Apple" = true (μ•„μŠ€ν‚€, μœ λ‹ˆμ½”λ“œλ‘œ 계산, iκ°€ 더큼)
  • "Application" > "Steve Jobs" = false (Aκ°€ 더 큼)
  • "Swift5" <= "Swift4" = false (SwftκΉŒμ§€ λͺ¨λ‘ λ™μΌν•˜λ‹€κ°€ λ§ˆμ§€λ§‰ μˆ˜μ—μ„œ 갈림)
  • "Playground" == "Playground" true

Logical Operators

// Logical AND Operator
true && true  //t
true && false //f
false && true //f
false && false //f 

// Logical OR Operator
true || true //t
true || false //t
false || true //t
false || false //f

// Logical Negation Operator
!true  //f
!false //t


// Combining Logical Operators
let enteredDoorCode = true  //t
let passedRetinaScan = false  //f
let hasDoorKey = false  //f
let knowsOverridePassword = true //t



// λ‹€μŒ λ…Όλ¦¬μ‹μ—μ„œ μ–΄λ–€ 상황일 λ•Œ "Open the door"κ°€ 좜λ ₯λ κΉŒμš”?

if enteredDoorCode && passedRetinaScan || hasDoorKey || knowsOverridePassword {
  print("Open the door")
} else {
  print("Can't open the door")
}

// Explicit Parentheses
if (enteredDoorCode && passedRetinaScan) || hasDoorKey || knowsOverridePassword {
  // ...
} else {
  // ...
}

Question

  • μ˜ˆμ‹œλ₯Ό 톡해 μž‘λ™μˆœμ„œλ₯Ό μ•Œμ•„λ΄…μ‹œλ‹€.
  • μš°μ„ μˆœμœ„ : && > ||
func returnTrue() -> Bool {
  print("function called")
  return true
}

// μ•„λž˜ 3개의 μΌ€μ΄μŠ€μ—μ„œ returnTrue λ©”μ„œλ“œλŠ” 각각 λͺ‡ λ²ˆμ”© 호좜될까?

print("\n---------- [ Case 1 ] ----------\n")
returnTrue() && returnTrue() && false || true && returnTrue()
//3번.------------- λ‘κ°œλ¨Όμ € κ³„μ‚°λ˜μ–΄ νŽ„μŠ€, 뒀에 νŽ„μŠ€ μ°Έ, 뒀에 or κ·Έ 전에 μ°Έ||μ°Έ = μ°Έ
print("\n---------- [ Case 2 ] ----------\n")
returnTrue() && false && returnTrue() || returnTrue()
//2번. ------------------------------3개 λ¨Όμ € 트루둜 계산, 결과적으둜 μ°Έ||μ°Έ = μ°Έ
print("\n---------- [ Case 3 ] ----------\n")
returnTrue() || returnTrue() && returnTrue() || false && returnTrue()
//3번.---------------------------------------맨 μ•žμ— 참이면 뒀에 ||뒀에 $$가와도 μ°Έ 이후 &&μ—μ„œ 참갈림.
// (1+2+3)*4 = μ΄λŸ°μ‹μœΌλ‘œ.

Range Operators

print("\n---------- [ Range Operators ] ----------\n")

// Closed Range Operator
0...100

for index in 1...5 {
  print("\(index) times 5 is \(index * 5)")
}


// Half-Open Range Operator
0..<100

let names = ["Anna", "Alex", "Brian", "Jack"]
let count = names.count
for i in 0..<count {   // 0, 1, 2, 3
  print("Person \(i + 1) is called \(names[i])")
}

// One-Sided Ranges
1...
...100
..<100


//               0       1        2       3
//let names = ["Anna", "Alex", "Brian", "Jack"]
names[2...] //2이상
names[...2] //2μ΄ν•˜
names[..<2] //2보닀 μž‘μ€ 인덱슀



//  μˆœμ„œλ₯Ό λ°˜λŒ€λ‘œ μ μš©ν•˜λŠ” 방법
for index in (1...5) {
  print("\(index) times 5 is \(index * 5)")
}

// 1) reversed
for index in (1...5).reversed() {
  print("\(index) times 5 is \(index * 5)")
}
print()

// 2) stride
for index in stride(from: 5, through: 1, by: -1) {
  print("\(index) times 5 is \(index * 5)")
}
print()
  • reversed 같은 λ©”μ„œλ“œλ₯Ό 쓰지 μ•Šκ³  μ—­μˆœμœΌλ‘œ 좜λ ₯되게 ν•˜κΈ°
let range = 1...5
range.lowerBound   // ν•˜ν•œμ„ 
range.upperBound   // μƒν•œμ„ 

for index in range {
  // 1 : 5 - 1 + 1 = 5
  // 2 : 5 - 2 + 1 = 4
  // ...
  let num = range.upperBound - index + range.lowerBound
  print("\(num) times 5 is \(num * 5)")
}