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