swift 0x02 기본 연산자
DESCRIPTION
애플 스위프트 언어 기본연산자에 대한 내용입니다. 기본적인 뼈대는 http://swift.leantra.kr/ 를 기반으로 합니다.TRANSCRIPT
상수와변수, 타입명시, 주석, 세미콜론, 정수,
부동소수점, 타입세이프와타입추정, 숫자의문자표현, 숫자의타입변환, 타입알리아스, 이진형, 튜플, 옵셔널, IF문과강제언래핑, 옵셔널바
인딩, nil, 무조건적인언래핑된옵셔널,
Assertions
목차
상수와변수
let = 상수var = 변수
타입명시
var welcomeMessage: String
//초기값이없으면타입추정이불가능하다
주석
//주석, /* 주석 */
세미콜론
let cat = “냐옹"; println(cat);
let cat = “냐옹"
println(cat)
정수
let minValue = UInt8.min
let maxValue = UInt8.max
정수
32 bit / Int = Int32
64bit / Int = Int64
-2,147,483,648 ~ 2,147,483,647
Double = 64bit ( 최소 15자리소수부)
Float = 32bit ( 6자리소수부 )
부동소수점
컴파일시점에서타입검사를수행.
타입을고민할필요가없다.
타입세이프
초기값과함께선언할때타입추정은유용하다.
부동소수점을표현할때는 Double을우선한다.
타입추정
2진수 0b / 0b10001
8진수 0o / 0o21
16진수 0x / 0x11
숫자의문자표현
1.25e2 = 125.0
1.25e-2 = 0.0125
0xFp2 = 15 x 2^2 = 60.0
0xFp-2 = 15 x 2^-2 = 3.75
숫자의문자표현
1_000_000
= 1000000
= 0000001000000
숫자의문자표현
let twoThousand: UInt16 = 2_000
let one: UInt8 = 1
let twoThousandAndOne = twoThousand + UInt16(one)
or
let twoThousandAndOne = twoThousand + one
정수형변환
let three = 3
let pointOneFourOneFiveNine = 0.14159
let pi = Double(three) + pointOneFOurOneFiveNine
let integerPi = Int(pi)
정수와실수변환
typealias AudioSample = UInt16
var maxAmplitudeFound = AudioSample.min
타입알리아스
let orangesAreOrange = true
let turnipsAreDelicious = false
let i = 1
if i {
println(“am i right?”)
}
이진형
let http404Error = (404, “Not Found”)
// type (Int, String)
튜플
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”
튜플
let (statusCode, _) = http404Error
println(“The status code is \(statusCode)”)
// prints “The status code is 404”
튜플
println(“The status code is \(http404Error.0)”)
// prints “The status code is 404”
튜플
let http200Status = (statusCode: 200, description: “OK”)
println(“The status code is \(http200Status.statusCode)”
//prints “The status code is 200”
튜플
let possibleNumber = “123”
let convertedNumber = possibleNumber.toInt()
convertedNumber는 “Int?” 또는 “optional Int”
옵셔널(Optionals)
옵셔널(Optionals)
옵셔널바인딩(Optional Binding)
값이없는상태의옵셔널변수 = nil
nil은옵셔널이아닌상수나변수에사용할수없다.
옵셔널의초기값은기본적으로 nil이다.
nil
Swift의 nil != Object-C의 nil
Object-C의 nil = null.
Swift의 nil = 명시적으로값이없음을나타낸다.
nil
옵셔널?
possibleString: String? = “An optional string.”
옵셔널언랩핑?
possibleString!
항상언랩핑된옵셔널
항상언랩핑된옵셔널
Assertion
Thanks!