전체보기
-
[백준 1000][Swift] 두 정수 A와 B를 입력받은 다음, A+B를 출력알고리즘 2022. 3. 31. 10:37
코드 import Foundation let input = readLine()?.components(separatedBy: " ") if let a = Int(input?.first ?? "0"), let b = Int(input?.last ?? "0") { print(a + b) } 혹은 let input = readLine()?.split(separator: " ") let first = Int(input?.first ?? "0")! let second = Int(input?.last ?? "0")! print(first + second) https://www.acmicpc.net/problem/1000 1000번: A+B 두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오. ..
-
[Swift] Optional(옵셔널)Swift 2022. 3. 17. 20:58
옵셔널은 enum이다. enum Optional { case None // not set case Some // set, T는 연관값 } Optional에서 는 옵셔널이 모든 타입의 옵셔널이 될 수 있다는 뜻 let x: String? = nil // = Optional.None let x: String? = "hello" // = Optional.Some("hello"), 연관값 hello 옵셔널을 unwrapped 하는 방법 1. switch를 사용해서 옵셔널을 unwrapped해 연관값을 가져오는 방법 switch x { case Some(let value): y = value case None: // error: raise an exception } 값이 없으면 런타임 오류가 발생함 2. if l..