상수와 변수(Constants and Variables)
상수와 변수는 이름과 특정 타입의 값을 연결합니다. 상수(Constant)의 값은 최초 지정 후 변경이 불가능하지만 변수(Variable)는 다른 값으로 변경이 가능합니다. 상수는 let 키워드로 선언하고, 변수는 var 키워드로 선언합니다.
// 상수
let str = "Constants"
// 변수
var str = "Hello Swift"
str = "Variable"
타입 명시(Type Annotations)
상수 또는 변수를 선언할 때 타입을 명확하게 하기 위해 타입 명시를 할 수 있습니다. 타입을 명시하는 방법은 상수 또는 변수의 이름 뒤에 콜론과 공백 한칸 뒤에 사용할 타입을 적으면 됩니다.
var str: String = "Hello Swift"
또한, 아래와 같이 마지막 변수 뒤에 타입을 명시하면 같은 타입의 여러개의 변수를 한번에 선언할 수 있습니다.
var red, green, blue: Double
상수와 변수의 이름(Naming Constants and Variables)
상수와 변수 이름은 Unicode) 문자를 포함하여 대부분의 문자를 포함할 수 있습니다. 다만, 숫자로 시작하는 일므은 선언할 수 없습니다.
let n = 3.14159
let 你好 = "你好世界"
let 🐶🐮 = "dogcow"
세미콜론(Semicolons)
일반적으로는 Swift는 각 구문 후의 세미콜론(;)은 필수조건이 아니지만, 여러 구문을 한 줄에 사용할 경우에는 세미콜론을 필수로 작성해야 합니다.
let cat = "🐱"; print(cat)
// Prints "🐱"
정수(Integers)
정수(Integers)는 부호가 있는 정수(signed)(양수, 0, 음수) 또는 부호가 없는 정수(unsigned)(양수, 0)이 있습니다.
Swift는 8, 16, 32, 64 비트 형태의 부호가 있는 정수와 부호가 없는 정수를 지원합니다. 이러한 정수는 8-bit 부호가 없는 정수는 UInt8, UInt32 등의 네이밍 형태를 가집니다.
정수 범위(Integer Bounds)
각 정수 타입의 min과 Max 프로퍼티를 통해 각 정수 타입의 최소값과 최대값을 가져올 수 있습니다.
let minValue = UInt8.min // minValue is equal to 0, and is of type UInt8
let maxValue = UInt8.max // maxValue is equal to 255, and is of type UInt8
타입 별칭(Type Aliases)
타입 별칭은 이미 존재하는 타입을 다른 이름으로 정의합니다. typealias 키워드를 사용하여 정의할 수 있습니다.
typealias AudioSample = UInt16
var maxAmplitudeFound = AudioSample.min
// maxAmplitudeFound is now 0
부울 (Booleans)
Swift는 Bool 이라 불리는 기본 부울 (Boolean) 타입이 존재합니다.
let orangesAreOrange = true
let turnipsAreDelicious = false
if turnipsAreDelicious {
print("Mmm, tasty turnips!")
} else {
print("Eww, turnips are horrible.")
}
// Prints "Eww, turnips are horrible."
let i = 1
if i {
// this example will not compile, and will report an error
}
let i = 1
if i == 1 {
// this example will compile successfully
}
튜플 (Tuples)
튜플(Tuples)는 여러 값을 단일 복합 값으로 그룹화 합니다. 튜플안에 값은 어떠한 타입도 가능하며 서로 같은 타입일 필요는 없습니다.
아래 예제의 (404, "Not Found")는 HTTP 상태 코드를 나타내는 튜플입니다.
let http404Error = (404, "Not Found")
// http404Error is of type (Int, String), and equals (404, "Not Found")
위 튜플의 타입은 (Int, String)입니다. 그러므로 아래처럼 별도의 상수 또는 변수로 분해(decompose)하여 정의할 수 있습니다.
let (statusCode, statusMessage) = http404Error
print("The status code is \(statusCode)")
// Prints "The status code is 404"
print("The status message is \(statusMessage)")
// Prints "The status message is Not Found"
튜플의 값 중 일부만 필요한 경우 튜플을 분해할 때 밑줄(_)로 튜플의 일부를 무시할 수 있습니다.
let (justTheStatusCode, _) = http404Error
print("The status code is \(justTheStatusCode)")
// Prints "The status code is 404"
또는 0에서 시작하는 인덱스를 사용하여 튜플의 개별 요소 값에 접근할 수 있습니다.
print("The status code is \(http404Error.0)")
// Prints "The status code is 404"
print("The status message is \(http404Error.1)")
// Prints "The status message is Not Found"
튜플을 정의할 때 튜플의 요소에 이름을 정할 수 있습니다. 그래서, 튜플 요소에 이름이 있다면 요소의 값에 요소 이름으로 접근이 가능합니다.
let http200Status = (statusCode: 200, description: "OK")
print("The status code is \(http200Status.statusCode)")
// Prints "The status code is 200"
print("The status message is \(http200Status.description)")
// Prints "The status message is OK"
옵셔널 (Optionals)
값이 없는 경우에는 옵셔널을 사용합니다.
옵셔널은 2가지가 존재합니다. 지정된 타입의 값이 있고, 누락될 수 있는 값이 있습니다.
누락될 수 있는 값의 예시로 String의 변수를 Int 값으로 변환하는 초기화가 있습니다. 일부 문자열만 정수로 변환할 수 있습니다. 아래 예제처럼 문자열 "123"은 숫자값 123으로 변환할 수 있지만, 문자열 "hello, world"는 변환할 숫자값이 없습니다.
let possibleNumber = "123"
let convertedNumber = Int(possibleNumber)
// The type of convertedNumber is "optional Int"
위의 코드에서 초기화가 실패할 수 있으므로 Int가 아닌 optional Int 타입인 Int? 타입을 반환합니다.
nil
옵셔널 변수에 특수한 값 nil로 지정하여 값이 없는 상태를 나타낼 수 있습니다.
var serverResponseCode: Int? = 404
// serverResponseCode contains an actual Int value of 404
serverResponseCode = nil
// serverResponseCode now contains no value
아래처럼 기본값 없이 옵셔널 변수를 정의하면 이 변수는 자동적으로 nil로 설정됩니다.
var surveyAnswer: String?
// surveyAnswer is automatically set to nil