Swift 0x02 기본 연산자

32
Swift - 0x02 기본연산자 아꿈사 - 문현진 ([email protected] )

description

애플 스위프트 언어 기본연산자에 대한 내용입니다. 기본적인 뼈대는 http://swift.leantra.kr/ 를 기반으로 합니다.

Transcript of Swift 0x02 기본 연산자

Page 1: Swift 0x02   기본 연산자

Swift - 0x02

기본연산자

아꿈사 - 문현진 ([email protected])

Page 2: Swift 0x02   기본 연산자

상수와변수, 타입명시, 주석, 세미콜론, 정수,

부동소수점, 타입세이프와타입추정, 숫자의문자표현, 숫자의타입변환, 타입알리아스, 이진형, 튜플, 옵셔널, IF문과강제언래핑, 옵셔널바

인딩, nil, 무조건적인언래핑된옵셔널,

Assertions

목차

Page 3: Swift 0x02   기본 연산자

상수와변수

let = 상수var = 변수

Page 4: Swift 0x02   기본 연산자

타입명시

var welcomeMessage: String

//초기값이없으면타입추정이불가능하다

Page 5: Swift 0x02   기본 연산자

주석

//주석, /* 주석 */

Page 6: Swift 0x02   기본 연산자

세미콜론

let cat = “냐옹"; println(cat);

let cat = “냐옹"

println(cat)

Page 7: Swift 0x02   기본 연산자

정수

let minValue = UInt8.min

let maxValue = UInt8.max

Page 8: Swift 0x02   기본 연산자

정수

32 bit / Int = Int32

64bit / Int = Int64

-2,147,483,648 ~ 2,147,483,647

Page 9: Swift 0x02   기본 연산자

Double = 64bit ( 최소 15자리소수부)

Float = 32bit ( 6자리소수부 )

부동소수점

Page 10: Swift 0x02   기본 연산자

컴파일시점에서타입검사를수행.

타입을고민할필요가없다.

타입세이프

Page 11: Swift 0x02   기본 연산자

초기값과함께선언할때타입추정은유용하다.

부동소수점을표현할때는 Double을우선한다.

타입추정

Page 12: Swift 0x02   기본 연산자

2진수 0b / 0b10001

8진수 0o / 0o21

16진수 0x / 0x11

숫자의문자표현

Page 13: Swift 0x02   기본 연산자

1.25e2 = 125.0

1.25e-2 = 0.0125

0xFp2 = 15 x 2^2 = 60.0

0xFp-2 = 15 x 2^-2 = 3.75

숫자의문자표현

Page 14: Swift 0x02   기본 연산자

1_000_000

= 1000000

= 0000001000000

숫자의문자표현

Page 15: Swift 0x02   기본 연산자

let twoThousand: UInt16 = 2_000

let one: UInt8 = 1

let twoThousandAndOne = twoThousand + UInt16(one)

or

let twoThousandAndOne = twoThousand + one

정수형변환

Page 16: Swift 0x02   기본 연산자

let three = 3

let pointOneFourOneFiveNine = 0.14159

let pi = Double(three) + pointOneFOurOneFiveNine

let integerPi = Int(pi)

정수와실수변환

Page 17: Swift 0x02   기본 연산자

typealias AudioSample = UInt16

var maxAmplitudeFound = AudioSample.min

타입알리아스

Page 18: Swift 0x02   기본 연산자

let orangesAreOrange = true

let turnipsAreDelicious = false

let i = 1

if i {

println(“am i right?”)

}

이진형

Page 19: Swift 0x02   기본 연산자

let http404Error = (404, “Not Found”)

// type (Int, String)

튜플

Page 20: Swift 0x02   기본 연산자

let (statusCode, statusMessage) = http404Error

println(“The status code is \(statusCode)”)

// prints “The status code is 404”

println(“The status message is \(statusMessage)”)

// prints “The status message is Not Found”

튜플

Page 21: Swift 0x02   기본 연산자

let (statusCode, _) = http404Error

println(“The status code is \(statusCode)”)

// prints “The status code is 404”

튜플

Page 22: Swift 0x02   기본 연산자

println(“The status code is \(http404Error.0)”)

// prints “The status code is 404”

튜플

Page 23: Swift 0x02   기본 연산자

let http200Status = (statusCode: 200, description: “OK”)

println(“The status code is \(http200Status.statusCode)”

//prints “The status code is 200”

튜플

Page 24: Swift 0x02   기본 연산자

let possibleNumber = “123”

let convertedNumber = possibleNumber.toInt()

convertedNumber는 “Int?” 또는 “optional Int”

옵셔널(Optionals)

Page 25: Swift 0x02   기본 연산자

옵셔널(Optionals)

Page 26: Swift 0x02   기본 연산자

옵셔널바인딩(Optional Binding)

Page 27: Swift 0x02   기본 연산자

값이없는상태의옵셔널변수 = nil

nil은옵셔널이아닌상수나변수에사용할수없다.

옵셔널의초기값은기본적으로 nil이다.

nil

Page 28: Swift 0x02   기본 연산자

Swift의 nil != Object-C의 nil

Object-C의 nil = null.

Swift의 nil = 명시적으로값이없음을나타낸다.

nil

Page 29: Swift 0x02   기본 연산자

옵셔널?

possibleString: String? = “An optional string.”

옵셔널언랩핑?

possibleString!

항상언랩핑된옵셔널

Page 30: Swift 0x02   기본 연산자

항상언랩핑된옵셔널

Page 31: Swift 0x02   기본 연산자

Assertion

Page 32: Swift 0x02   기본 연산자

Thanks!