[스위프트 프로그래밍-3장] 데이터 타입 기본

sanghee·2021년 9월 13일
0
post-thumbnail

이 글은 스위프트 프로그래밍(3판, 야곰 지음)을 읽고 간단하게 정리한 글입니다. 책에 친절한 설명과 관련 예제 코드들이 있으므로 직접 사서 읽기를 추천합니다.

3.0 소개

데이터 타입은 프로그램 내에서 쓰이는 데이터 종류를 의미한다. 스위프트는 구조체를 기반으로 데이터가 구현되어 있다.

3.1 Int와 UInt

Int 정수이고 UInt는 양의 정수이다. 최댓값과 최솟값은 각각 max, min 프로퍼티로 알 수 있다. 각각 8비트, 16비트 32비트, 64비트 형태가 있다. 시스템 아키텍쳐에 따라 Int는 Int32 또는 Int64이며 UInt또한 마찬가지이다. UInt.max보다 작지만 Int.max보다 커 Int를 쓰지 못하는 경우가 아니라면 Int를 사용한다.

코드에서 UInt.max가 Int.max보다 더 큰 양의 정수임을 확인할 수 있다. Int.max와 Int64.max가 같은 것으로 보아, 시스템이 64비트인 것을 확인할 수 있다.

var integer: Int = -100
let uInteger: UInt = 50

let intMax = Int.max // 9223372036854775807
let intMin = Int.min // 9223372036854775808

let uIntMax = UInt.max // 18446744073709551615
let uIntMin = UInt.min // 0

print(UInt.max > Int.max) // true

let largeInt: Int64 = Int64.max // 9223372036854775807
let smallUInt: UInt8 = UInt8.min // 0

print(Int.max == Int64.max) // true

integer = Int(uInteger) // 50

3.2 Bool

Bool은 불리언 타입으로 true 또는 false이다.

3.3 Float과 Double

Float과 Double은 부동소수 타입이다. Float이 6자리의 십진수까지만 표현이 가능한 반면 Double은 15자리까지 가능하다. e는 exponent로 지수를 뜻한다.

var float: Float = 1234567.8 // 1.234568e+09
float = 12345678.9 // 1.234568e+07

var double: Double = 1234567890.1 // 1234567890.1

float = 123456.7 // 123456.7
float = 123456.78 // 123456.8
double = 123456.789 // 123456.789

3.4 Character

Character은 문자이다. 스위프트는 *유니코드 9 문자를 사용한다.

*유니코드 9: 유니 코드 9.0 버전을 의미하며 이모티콘을 포함하여 2016년에 출시되었다.

let character: Character = "A" // "A"

3.5 String

String은 문자열이다. 마찬가지로 유니코드 9 문자를 사용한다. 여러 줄을 직접 쓰고 싶다면 """을 아래와 같이 사용하면 아래에 나올 줄바꿈, 탭 특수문자를 생략할 수 있다.

let word: String = "swift"
let paragraph: String = """
        swift
    programming
    """

let greeting: String = "hello"
let name: String = "world"

let introduce = greeting + ", " + name // "hello, world"

greeting.hasPrefix("h") // true
greeting.hasSuffix("o") // true

introduce.hasPrefix("h") // true
introduce.hasSuffix("hello") // false
introduce.hasSuffix("rld") // true

introduce.uppercased() // "HELLO, WORLD"
introduce.lowercased() // "hello, world"

introduce.count // 12

3.5.1 특수문자

특수문자는 백슬래시에 특정한 문자를 조합하여 사용한다. \n은 줄바꿈, \은 백슬래쉬, \t은 탭, \0은 문자열이 끝났음을 알린다.

var introduce = "hello, world"

introduce = "hello, \nworld"
// hello, 
// world

introduce = "hello, world\\" // hello, world\
print(introduce)

introduce = "hello, world\"" // hello, world"
print(introduce)

introduce = "\thello, world" // hello, world
print(introduce)

introduce = "hello, world\0" // hello, world
print(introduce)

+ 문자열 보간법(String Interpolation)

문자열에 변수를 포함하여 표현하고 싶을 때 문자열 내에 (변수)와 작성한다.

let name: String = "unknown"
let introduce: String = "hello, \(name)" // "hello, unknown"

+ 문자열 쪼개어 배열로

문자열을 쪼개어 배열로 만들려고 한다. map함수를 쓰거나 indices 프로퍼티를 이용할 수 있다. 컬렉션형 타입에는 indices 프로퍼티를 가지는데 인덱스 범위를 의미한다.

let str: String = "string"

let strArr1 = str.map({ $0 }) // ["s", "t", "r", "i", "n", "g"]
var strArr2: [String] = [] // ["s", "t", "r", "i", "n", "g"]

for i in str.indices {
    strArr2.append(String(str[i]))
}

3.6 Any, AnyObject와 nil

Any는 모든 데이터 타입을 의미한다. AnyObject는 Any보다는 한정적으로 클래스의 인스턴스에만 할당할 수 있다. nil은 타입은 아니고 없음을 의미하는 키워드이다.

var any: Any = "a"
any = 1
profile
👩‍💻

0개의 댓글