[swift] playground, Tuple, Boolean, Scope

2023. 2. 13. 16:43👩🏻‍💻 ios 앱개발 ( swift )

728x90
import UIKit

var greeting = "Hello, playground"

let randomNum = arc4random_uniform(100)

/*
 여러 줄을 남길 때
 설명이 길 때
 */

//[tuple]
let coordinates = (4,6)

let x = coordinates.0
let y = coordinates.1

let coordinatesNamed = (x: 4, y: 6)
let x2 = coordinatesNamed.x
let y2 = coordinatesNamed.y

let (x3, y3) = coordinatesNamed
x3
y3

let coordinatesNamed3=(x:1, y:2, z:3)
let (x4, y4, z4) = coordinatesNamed3
x4
y4
z4

let yes = true
let no = false

let isFourGreaterThanFive = 4>5

if isFourGreaterThanFive{
    print("참이다")
}else{
    print("거짓이다")
}

//if 조건....{
//    //조건이 만족할 때 수행해야 하는 코드
//} else{
//    //나머지는 여기서 ...
//}

let a = 5
let b = 10

if a>b {
    print("a win")
} else {
    print("b win")
}


let name1 = "Jin"
let name2 = "Jason"

let isTwoNameSame = (name1 == name2)

if isTwoNameSame{
    print("같은 이름일세")
}else{
    print("다른 이름일세")
}

let isJason = (name2 == "Jason")

let jasonAndSameName = isTwoNameSame && isJason
let jasonOrSameName = isTwoNameSame || isJason

var title = ""

if isJason {
    title = "JASON CEO"
}else {
    title = "Other CEO"
}

title = isJason ? "JASON CEO" : "Other CEO"

let name3 = "Joohyun"
let name4 = "geehoo"

let isAwsome = (name3 == "joohyun")

let AwsomeGirl = isAwsome ? name3 : name4
print("Awsome girl is",AwsomeGirl)
import UIKit

//[scope]
//예제 : 알바해서 시급받기

var hours = 50
let payPerHour = 10000
var salary = 0

if hours>40 { //40시간 초과되는 시간은 원래시급의 두배로 쳐줘야함
    let extraHours = hours - 40
    salary += extraHours * payPerHour*2
    hours -= extraHours
}

salary += hours * payPerHour

//print(extraHours)
//scope을 벗어난 변수를 밖에서 출력했기 때문에 error

 

'👩🏻‍💻 ios 앱개발 ( swift )' 카테고리의 다른 글

[swift] optional  (0) 2023.02.16
[ swift ] Function  (0) 2023.02.14
[swift] switch문  (0) 2023.02.13
[ swift ] for loop, import foundation, closedRange  (0) 2023.02.13
[swift] flow Control_while loop, repeat  (0) 2023.02.13