티스토리 뷰

< Literals & Types >



* 상수 ?


 고정된 값(메모리 주소)을 가지는 심볼 / 식별자


* 리터럴 ?


 소스코드에서 고정된 값으로 표현되는 문자 (데이터) 그 자체

 예를 들면 정수, 실수, 문자, 문자열, 불리언 리터럴 등




******** 결과는 Xcode를 통해 확인해보세요 ********




1) 숫자 리터럴(Numeric Literals)



var signedInteger : Int = 123       

signedInteger = +123

signedInteger = -123

type(of: signedInteger)



let decimalInteger = 17              // 10진법 표현(Basic)

let binaryInteger = 0b10001       // 2진법 표현

type(of: binaryInteger)

let octaInteger = 0o21               // 8진법 표현

let hexadecimalInteger = 0x11   // 16진법 표현



* Reference. 숫자 표현할 때 1000000000000 이 몇자리 수 인지 계산하기 어려울 때를 위해 아래와 같이 지원한다.


var bigNumber = 1_000_000_000             // 언더바 위치는 마음대로 가능. 일반적으로 3자리 수

bigNumber = 000_001_000_000_000     // 1전의 0은 무시

bigNumber = 0b1000_1000_0000         // 2진수

bigNumber = 0x00_FF_00_FF             // 16진수 (2자리나 4자리로 끊음)




 * Reference. 정수 타입(Integer Types) 의 종류에 대하여

 


1. 8-bit : Int8, UInt8

2. 16-bit : Int16, UInt16

3. 32-bit : Int32, UInt32

4. 64-bit : Int64, UInt64 

5. Platform dependent : Int, UInt (64-bit on modern devices) 


* Examples


var integer = 123

integer = -123

type(of: integer)



var unsignedInteger : UInt = 123

//unsignedInteger = -123 : 오류(UInt는 양의 정수 범위만을 다루기 때문이다 !)

type(of: unsignedInteger)


MemoryLayout<Int>.size // 32비트나 다른 비트에서 사용하면 사이즈가 디바이스에 따라 4가 될수도 있음.

Int.max // 최대값 제공

Int.min // 최소값 제공



MemoryLayout<UInt>.size

UInt.max // 오른쪽에 출력되는 값은 Xcode상 버그 때문

UInt.min

print(UInt.max)



MemoryLayout<Int8>.size

Int8.max

Int8.min



MemoryLayout<UInt8>.size

Int16.max

Int16.min



MemoryLayout<Int32>.size

Int32.max

Int32.min



MemoryLayout<UInt32>.size

UInt32.max

UInt32.min



MemoryLayout<Int64>.size

Int64.max

Int64.min 



MemoryLayout<UInt64>.size

UInt64.max

UInt64.min



// 차이 구분

let q2 : Int8 = 127 + 1 // 컴파일 과정에서 오류

let q3 = Int8(127+1) // 런타임 과정에서 오류



// 오버플로우 오류

Int32.max + 1

Int64.max + 1


 


* Overflow addition operator

  - 01111111 10000000 : 자동으로 최솟값이 된다


Example)


Int8.max &+ 1 

Int32.max &+ 1

Int64.max &+ 1



Int8.max &- 1

Int32.max &- 1

Int64.max &- 1  






2) 부동 소수점 리터럴(Floating-point Literal) 


var floatingPoint = 1.23

floatingPoint = 1.23e4         // 1.23 * 10^4

floatingPoint = 0xFp3         // 16진수, 15 * 2^3

type(of: floatingPoint)



var floatingPointValue = 1.23

type(of : floatingPointValue)



var floatValue : Float = 1.23         // 혹은 Float(1.23)

var floatValue1 : CGFloat = 1.23  // 혹은 CGFloat(1.23)



MemoryLayout<Float>.size

Float.greatestFiniteMagnitude     // FLT_MAX

Float.leastNormalMagnitude        // FLT_MIN



MemoryLayout<Double>.size

Double.greatestFiniteMagnitude // DBL_MAX

Double.leastNormalMagnitude    // DBL_MIN



* Reference


 Double - 최소 소수점 이하 15 자리수 이하의 정밀도

 Float - 최소 소수점 이하 6 자리수 이하의 정밀도

 부동 소수점이므로 소수점 이하의 정밀도는 변경 될 수 있음 !





3) 불리언 리터럴(Boolean Literal)



var isBool = true

type(of: isBool)



isBool = false

// isBool = false => 가능.

// isBool = 1 => 0과 1로 True와 False를 나타내는 것은 Objective-C 에서 가능하지만 Swift에서는 불가능.

// isBool = 0 => 0과 1로 True와 False를 나타내는 것은 Objective-C 에서 가능하지만 Swift에서는 불가능.



let shouldChange : Bool = true





4) 문자열 리터럴(String Literal)



let str = "Hello, playground"                 // 쌍따옴표"" 들어가면 자동적으로 스트링타입으로 인식.

type(of: str)



let str1 = ""

type(of: str1)



var language: String = "Swift"





5) 문자 리터럴(Character Literal)



var nonCharacter = "C" //  ASCII code 참고.

type(of: nonCharacter)



var character : Character = "C" // Character 타입은 타입을 명시적으로 작성해주지 않으면 String 타입으로 나온다.

type(of: character)



Q. 위의 결과가 왜 그럴까? 아래를 확인해보자

MemoryLayout<String>.size

MemoryLayout<Character>.size


character = " "

type(of: character)



Q. 요거는 왜 틀린건지 생각해보자.

// character = ""                                                                                                                                                 => 하나도 다루지 않기 때문

// character = "String"                                                                                                                                        => 여러 개의 글자를 다루기 때문





 + 타입 엘리어스(Typealias)



 문맥상 더 적절한 이름으로 기존 타입의 이름을 참조하여 사용하고 싶을 경우 사용

 typealias <#type name#> = <#type expression#>



typealias AudioSample = UInt16                                                                                                    // Tip ! Command + Control + 클릭 : 정의 보기


var maxAmplitudeFound = AudioSample.min

var maxAmplitudeFound1 = UInt16.min


type(of: maxAmplitudeFound)

type(of: maxAmplitudeFound1)


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함