swft 3 for c programmers : data type
TRANSCRIPT
Swift 3 : Data Types
군산대학교 컴퓨터정보통신공학부 컴퓨터정보공학전공
남 광 우
Swift 3 Tour and Language Guide by Apple꼼꼼한재은씨의 Swift 2 프로그래밍
Data Type 개요
• Swift에서모든데이터타입은객체
• Int , float 등모든 type이객체• C에서의 int는 atomic type• Java에서는 int Type과 Integer(객체)로구분사용
• 그래서..• 3.distanceTo( 5) : (O)• Int.max : (O)
Simple Values•변수와상수의선언
•타입의추정
var myVariable = 42 // 변수myVariable = 50
let myConstant = 42 // 상수
let implicitInteger = 70let implicitDouble = 70.0let explicitDouble: Double = 70
Simple Values•서로다른타입값의변환
•변수상수의이름은한글한자도가능
let label = "The width is "let width = 94let widthLabel = label + String(width)
var한글변수 = 3let ㅎㄱㅂㅅ = 5.3
기본 자료형
• Int• Int8, Int16, Int32, Int64
• Uint• UInt8, UInt16, UInt32, UInt64
• Double & Float• Bool• String• Character
기본 자료형
•자료형의선언과사용의세가지방법
// firstvar day = 7
// secondvar day : Intday = 7
// thirdvar day : Int = 7
// 다음은 error!!!!var day day = 7
닫힌 범위 연산자
•범위값을나타내는연산
• 1…5 : 1, 2, 3, 4, 5
• 1..<5 : 1, 2, 3, 4
• for 문에서의사용• hello 100번 print 하기
for i in 1…100{
print( “Hello” )}
문자열 template
•변수,상수값을대입하기위한 template• \ (이스케이프) 문자의사용
• hello world 1..<100 까지번호와함께찍기
let apples = 3let oranges = 5let appleSummary = "I have \(apples) apples."let fruitSummary = "I have \(apples + oranges) pieces of fruit.
Type Aliases
• Type 선언을할수있음
•객체메소드그대로사용가능
typealias AudioSample = UInt16
var maxAmplitudeFound = AudioSample.min// maxAmplitudeFound is now 0
Tuple• Tuple
• 다수개의쌍으로표현되는데이터모음타입• return 타입으로사용가능
• 선언
•사용예
let http404Error = (404, "Not Found")// http404Error is of type (Int, String), and equals (404, "Not Found")
let (statusCode, statusMessage) = http404Errorprint("The status code is \(statusCode)")// Prints "The status code is 404"print("The status message is \(statusMessage)")// Prints "The status message is Not Found"
Tuple• Tuple에서의 _
• Tuple에서의 individual element name
let (justTheStatusCode, _) = http404Errorprint("The status code is \(justTheStatusCode)")// Prints "The status code is 404"
let http200Status = (statusCode: 200, description: "OK")
print("The status code is \(http200Status.statusCode)")// Prints "The status code is 200“
print("The status message is \(http200Status.description)")// Prints "The status message is OK"
Tuple• Tuple에서의 Index 사용
let http200Status = (statusCode: 200, description: "OK")
print("The status code is \(http404Error.0)")// Prints "The status code is 404"print("The status message is \(http404Error.1)")// Prints "The status message is Not Found"