본문 바로가기
모바일앱/iOS

옵셔널 복습

by GeekCode 2021. 11. 18.
반응형

고급기능 4가지

// Forced unrapping 

// Optional binding (if let)

// Optional binding2 (guard)

// Nill coalescing

 

import SwiftUI

var carName: String? = "땡크"
//고급기능 4가지

// Forced unrapping
// Optional binding (if let)
// Optional binding2 (guard)
// Nill coalescing

// 영어로 공부하는 이유 : 공부하다가 막히는 경우 영어로 검색하면 보다 다양한 루트를 발견가능


// Forced unrapping > 박스안에 있는 값을 꺼내서 사용할 때, 강제로 여는 것
// Optional binding (if let) > 부드럽게 박스를 까는 방법 1
// Optional binding2 (guard) > 부드럽게 박스를 까는 방법 2
// Nill coalescing > 박스를 까봤더니 값이 없으면 디폴트 값을 줘보자 (중고나라 사기 -> 벽돌)

//print(carName)
//Optional("땡크")

//print(carName!)
//"땡크"

//carName = nil
//print(carName!)
//error: Execution was interrupted, reason: EXC_BREAKPOINT (code=1, subcode=0x18f2ea5d8).
// !로 강제로 언레핑이 가능하지만 안에 nil이라면 경고가 뜬다.


//if let unwrappedCarName = carName {
//    print(unwrappedCarName)
//} else {
//    print("Car Name 없다.")
//}

// -----> 함수로 사용해보기 , 파싱하고 캐스팅하기
//func printParsedInt(from: String) {
//    if let parsedInt = Int(from) {
//        print(parsedInt)
//    } else{
//        print("Int로 컨버팅안된단다")
//    }
//}

//printParsedInt(from: "100")
//printParsedInt(from: "하이 데얼")
//

//func printParsedInt(from: String) {
//    if let parsedInt = Int(from) {
//        print(parsedInt)
//        if parsedInt > 0 {
//            print(parsedInt)
//        } else{
//
//        }
//    } else{
//        print("Int로 컨버팅안된단다")
//    }
//}
//레벨이 너무 많아지면 복잡해진다.




//
//func printParsedInt(from: String){
//    guard let parsedInt = Int(from) else {
//        print("Int로 컨버팅안된단다")
//        return
//    }
//    print(parsedInt)
//}
//printParsedInt(from: "100")
//
//


//let myCarName = carName ?? "모델 S"
//carName이 nil 이라면 모델 S 라는 디폴트 값을 가지게 된다.

 

 

반응형

'모바일앱 > iOS' 카테고리의 다른 글

UITextfield에 입력후 키보드 숨기기  (0) 2021.12.14
2주차 프로젝트02_프로필  (0) 2021.12.14
2주차 프로젝트01_웹브라우저  (0) 2021.12.14
UIKit 알아보기  (0) 2021.12.14
arc의 간단 개념  (0) 2021.11.17
구조체와 클래스 복습  (0) 2021.11.16
투두리스트앱 만들기(진행중)  (0) 2021.11.12
투두리스트앱 만들기(구현계획)  (0) 2021.11.11