
23-04-19
노트 작성을 위해 실행 불가능한 코드를 주석으로 사용할 수 있다. Swift 컴파일러는 컴파일 시 주석을 무시한다. Swift의 주석은 C와 비슷하다.
// This is a comment.
여러 줄의 주석은 다음과 같이 표시한다.
/* This is also a comment
but is written over multiple lines. */
C와 다른 점은, 여러 줄의 주석 안에 또 다른 여러 줄의 주석이 포함되어 중첩될 수 있다. 다음과 같이 표시한다.
/* This is the start of the first multiline comment.
/* This is the second, nested multiline comment. */
/*This is the end of the first multiline comment.*/
다른 언어와 달리, 코드의 각 statement 뒤에 세미콜론(;)을 붙이지 않아도 된다. 붙여도 상관은 없다. 단, 여러 분리된 statements를 한 줄로 쓸 때는 다음과 같이 쓸 수 있다.
let cat = "🐱"; print(cat)
// Prints "🐱"
Integers는 42, -23과 같이 분수가 아닌 정수이다. Integers는 signed(부호를 가짐, 양수와 0, 음수) 될 수도 있고 unsigned(부호를 가지지 않음, 양수와 0) 될 수도 있다.
Swift는 signed, unsigned된 Integer를 8, 16, 32, 64비트 형태로 제공한다. 8비트 unsigned Integer는 Uint8, 32비트 signed Integer는 Int32 와 같이 나타낸다.
min과 max 속성을 사용하여 각 Integer type의 최소, 최대값을 구할 수 있다.
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
대부분의 경우, 특정 크기의 integer를 선택하지 않아도 된다. Swift는 현재 사용하는 플랫폼과 같은 크기의 Int를 제공한다. 이는 UInt도 마찬가지임.
32비트 플랫폼에서 Int는 Int32와 같은 크기이다.
64비트 플랫폼에서 Int는 Int64와 같은 크기이다.
32비트 플랫폼에서 UInt 는 UInt32와 같은 크기이다.
64비트 플랫폼에서 UInt 는 UInt64와 같은 크기이다.
Floating-point Numbers는 3.141592, 0.1, -273.14 과 같이 분수 요소를 포함한 실수를 말한다.
작업 환경에 따라 적합한 유형을 사용하면 된다. 둘 다 적합할 때는 Double이 선호된다.
Swift는 기본적으로 자료형 오류에서 안전한 언어이다. 자료형을 명료하게 제시하여 실수로 다른 자료형을 넣는 일을 방지한다. 컴파일 과정에서 이와 관련된 오류를 빠르게 잡아내 개발 과정을 원활하게 만들어준다.
상수와 변수를 선언할 때 매 번 자료형을 지정해줘야 되는 것은 아니다. 필요한 자료형을 지정하지 않는다면, Swift는 type inference를 사용해 적합한 자료형을 추론한다.이는 컴파일러가 개발자가 제공한 값을 바탕으로 자동으로 자료형을 추론하도록 돕는다.
let meaningOfLife = 42
// meaningOfLife is inferred to be of type int
let pi = 3.141592
// pi is inferred to be of type Double
실수 값을 추론할 때, Swift는 항상 Float 이 아닌 Double을 선택한다.
let anotherPi = 3 + 0.141592
// anotherPi is inferred to be Double
정수와 실수 사이의 변환은 명료하게 이루어져야 함.
let three = 3
let pointOneFourOneFiveNine = 0.14159
let pi = Doulbe(three) + pointOneFourOneFiveNine
// pi equals 3.14159, and is inferred to be of type Double
let integerPi = Int(pi)
// integerPi equals 3, and is inferred to be of type Int
🌏 참고사이트
Swift Language Guide