Toy Project/Swift Language Syntax

1. swift basic

개발자킹콩 2021. 1. 21. 16:21

import Foundation

//tuple
let coordinates = (4, 6)

let x1 = coordinates.0
let y1 = coordinates.1
print(x1, y1)

let coordinatesNamed = (x:2, y:7)
print(coordinatesNamed.x, coordinatesNamed.y)

let (x3, y3) = coordinatesNamed
print(x3)
print(y3)

print() //if
let name1 = "Jin"
let name2 = "Jason"

//true를 변수로
let isTwoNameSame = name1 == name2

if isTwoNameSame{
  print("맞다")
} else{
  print("틀리다")
}
let greetingMassage: String
//선언후 대입가능, 조건연산자 사용사능

//range
let closeRange = 0...10 //0~10
let halfClosedRange = 0..<10 //0~9

var sinValue: CGFloat = 0
for i in closeRange{

  sinValue = sin(CGFloat.pi/4 * CGFloat(i))

 //여기서 그림이 나옴 // import Foundation
}
for i in closeRange{
  if i % 2 == 0{
    print("짝수*")
  }
}
for i in closeRange where i % 2 == 0 {
  print("짝수&")
}

//switch
let num = 10
switch num { //모든수를 cover해야한다.
case 0:
  print("0이다")
case 0...10:
  print("0~10입니다.")
case 10:
  print("10입니다.")
default:
  print("나머지입니다.")
}
//range를 조건에 사용할 수 있다.
//해당 case를 검사 후 자동 break
let pet = "bird"
switch pet {
case "dog", "bird":
  print("is dog")
default :
  print("잘 모르겠습니다")
}
//여러 case를 묶어서 사용할 수 있다.
let number = 20
switch number {
case _ where number % 2 == 0:
  print("짝수다")
default:
  print("홀수다.")
}
//case로 where 조건문을 사용할 수 있다.

let coodinate = (x: 0, y: 10)
switch coodinate {
case (0,0):
  print("원점입니다.")
case (_,0):
  print("x축 위에 있습니다.")
case (0, _):
  print("y축 위에 있습니다.")
default:
  print("어딘가")
}
//tuple을 사용할 수 있으며 한쪽 좌표에 대한 조건을 사용할 수 있다.

let coodinate1 = (x: 10, y: 10)
switch coodinate1 {
case (0,0):
  print("원점입니다.")
case (_,0):
  print("x축 위에 있습니다.")
case (0, let y):
  print("y축 위에 있습니다.(0, \(y))")
case (let x, let y) where x==y:
  print("y=x 그래프 위에 있습니다. (\(x), \(y))")
default:
  print("어딘가")
}



'Toy Project > Swift Language Syntax' 카테고리의 다른 글

5. Swift Collection(Dicionary, Set)  (0) 2021.01.21
4. Swift Collection (Array)  (0) 2021.01.21
3. Swift Optional  (0) 2021.01.21
2. Swift Function  (0) 2021.01.21
0. 나의 개인 공부 페이지다  (0) 2021.01.21