SlideShare a Scribd company logo
1 of 10
Download to read offline
SWIFT
Types
Bill Kim(김정훈) | ibillkim@gmail.com
목차
•Types?
•Named type
•Compound Type
•Tuple Type
•Function Type
•Variadic Parameter
•References
Types?
Swift에서의 타입을 크게 보면 2가지로 분류할 수 있습니다.
named Type : 명명된 타입
compound Type : 복합(명명되지 않은) 타입
출처: https://noahlogs.tistory.com/13 [인생의 로그캣]
Named type
명명된 타입은 클래스, 구조체, 열거형과 프로토콜 등 이름이 있는
타입입니다.
Compound Type
복합 타입은 튜플과 함수 등과 같이 이름이 없는 타입입니다
Tuple Type
튜플은 기본 Swift내에 명명된 데이터 타입(Named Type)외에도
새롭게 선언한 튜플 타입도 저장할 수도 있습니다.
// 튜플 타입 정의
var greatTuple = (x: 10, y: 20)
print(greatTuple.0) // 10
print(greatTuple.x) // 10
typealias Point = (Int, Int) // typealias를 활요하여 튜플 타입에 이름을 지정할 수 있습니다.
var point = Point(10, 12)
print(point.1) // 12
Function Type
튜플처럼 함수 타입도 일반적으로 사용할 수 있습니다.
아무 것도 반환하고 싶지 않으면 아무 것도 쓰지 않으면 됩니다.
함수 이름과 중괄호 사이에 있는 것이 함수 타입입니다
func processPerson (withID: Int) -> () {}
func processVIP (withID: Int) {}
struct Person {
var firstname : String
var lastname : String
}
func nameForPerson1 (withID: Int) -> (name: String, age: Int) {
return ( "Bill", 20)
}
func nameForPerson2 (withID: Int) -> (Person) {
return (Person(firstname: "Joyce", lastname: “Cai"))
}
func nameForPerson3 (withID: Int) -> Person {
return Person(firstname: "Bob", lastname: "Lee")
}
func detailsForPerson (withID: Int) -> (obj: Person, age: Int) {
return (Person(firstname: "Bill", lastname: "Kim"), 10)
}
Variadic Parameter
Swift 에서도 여타 다른 언어들처럼 아래와 같이 가변 매개 변수
를 지원합니다.
// 함수 인자 앞에 ‘_’ 지시어를 붙이면 변수명을 생략할 수 있습니다.
func printAll1(_ numbers: Int...) {
for number in numbers {
print ( "the number (number)")
}
}
printAll1(0, 1, 2, 3, 4)
func printAll2( numbers: Int...) {
for number in numbers {
print ( "the number (number)")
}
}
printAll2(numbers: 0, 1, 2, 3, 4)
References
[1] Swift ) Types : https://zeddios.tistory.com/207
[2] Swift 타입의 비밀 : https://academy.realm.io/kr/
posts/altconf-2017-manu-rink-secret-life-of-types-in-
swift/
[3] Types : https://docs.swift.org/swift-book/
ReferenceManual/Types.html#//apple_ref/swift/
grammar/type
Thank you!

More Related Content

More from Bill Kim

[Algorithm] Sorting Comparison
[Algorithm] Sorting Comparison[Algorithm] Sorting Comparison
[Algorithm] Sorting ComparisonBill Kim
 
[Algorithm] Big O Notation
[Algorithm] Big O Notation[Algorithm] Big O Notation
[Algorithm] Big O NotationBill Kim
 
[Algorithm] Shell Sort
[Algorithm] Shell Sort[Algorithm] Shell Sort
[Algorithm] Shell SortBill Kim
 
[Algorithm] Radix Sort
[Algorithm] Radix Sort[Algorithm] Radix Sort
[Algorithm] Radix SortBill Kim
 
[Algorithm] Quick Sort
[Algorithm] Quick Sort[Algorithm] Quick Sort
[Algorithm] Quick SortBill Kim
 
[Algorithm] Heap Sort
[Algorithm] Heap Sort[Algorithm] Heap Sort
[Algorithm] Heap SortBill Kim
 
[Algorithm] Counting Sort
[Algorithm] Counting Sort[Algorithm] Counting Sort
[Algorithm] Counting SortBill Kim
 
[Algorithm] Selection Sort
[Algorithm] Selection Sort[Algorithm] Selection Sort
[Algorithm] Selection SortBill Kim
 
[Algorithm] Merge Sort
[Algorithm] Merge Sort[Algorithm] Merge Sort
[Algorithm] Merge SortBill Kim
 
[Algorithm] Insertion Sort
[Algorithm] Insertion Sort[Algorithm] Insertion Sort
[Algorithm] Insertion SortBill Kim
 
[Algorithm] Bubble Sort
[Algorithm] Bubble Sort[Algorithm] Bubble Sort
[Algorithm] Bubble SortBill Kim
 
[Algorithm] Binary Search
[Algorithm] Binary Search[Algorithm] Binary Search
[Algorithm] Binary SearchBill Kim
 
[Algorithm] Recursive(재귀)
[Algorithm] Recursive(재귀)[Algorithm] Recursive(재귀)
[Algorithm] Recursive(재귀)Bill Kim
 
[Swift] Data Structure - AVL
[Swift] Data Structure - AVL[Swift] Data Structure - AVL
[Swift] Data Structure - AVLBill Kim
 
[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Binary Search Tree[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Binary Search TreeBill Kim
 
[Swift] Data Structure - Graph(BFS)
[Swift] Data Structure - Graph(BFS)[Swift] Data Structure - Graph(BFS)
[Swift] Data Structure - Graph(BFS)Bill Kim
 
[Swift] Data Structure - Graph(DFS)
[Swift] Data Structure - Graph(DFS)[Swift] Data Structure - Graph(DFS)
[Swift] Data Structure - Graph(DFS)Bill Kim
 
[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Binary Tree[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Binary TreeBill Kim
 
[Swift] Data Structure - Tree
[Swift] Data Structure - Tree[Swift] Data Structure - Tree
[Swift] Data Structure - TreeBill Kim
 
[Swift] Data Structure - Graph
[Swift] Data Structure - Graph[Swift] Data Structure - Graph
[Swift] Data Structure - GraphBill Kim
 

More from Bill Kim (20)

[Algorithm] Sorting Comparison
[Algorithm] Sorting Comparison[Algorithm] Sorting Comparison
[Algorithm] Sorting Comparison
 
[Algorithm] Big O Notation
[Algorithm] Big O Notation[Algorithm] Big O Notation
[Algorithm] Big O Notation
 
[Algorithm] Shell Sort
[Algorithm] Shell Sort[Algorithm] Shell Sort
[Algorithm] Shell Sort
 
[Algorithm] Radix Sort
[Algorithm] Radix Sort[Algorithm] Radix Sort
[Algorithm] Radix Sort
 
[Algorithm] Quick Sort
[Algorithm] Quick Sort[Algorithm] Quick Sort
[Algorithm] Quick Sort
 
[Algorithm] Heap Sort
[Algorithm] Heap Sort[Algorithm] Heap Sort
[Algorithm] Heap Sort
 
[Algorithm] Counting Sort
[Algorithm] Counting Sort[Algorithm] Counting Sort
[Algorithm] Counting Sort
 
[Algorithm] Selection Sort
[Algorithm] Selection Sort[Algorithm] Selection Sort
[Algorithm] Selection Sort
 
[Algorithm] Merge Sort
[Algorithm] Merge Sort[Algorithm] Merge Sort
[Algorithm] Merge Sort
 
[Algorithm] Insertion Sort
[Algorithm] Insertion Sort[Algorithm] Insertion Sort
[Algorithm] Insertion Sort
 
[Algorithm] Bubble Sort
[Algorithm] Bubble Sort[Algorithm] Bubble Sort
[Algorithm] Bubble Sort
 
[Algorithm] Binary Search
[Algorithm] Binary Search[Algorithm] Binary Search
[Algorithm] Binary Search
 
[Algorithm] Recursive(재귀)
[Algorithm] Recursive(재귀)[Algorithm] Recursive(재귀)
[Algorithm] Recursive(재귀)
 
[Swift] Data Structure - AVL
[Swift] Data Structure - AVL[Swift] Data Structure - AVL
[Swift] Data Structure - AVL
 
[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Binary Search Tree[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Binary Search Tree
 
[Swift] Data Structure - Graph(BFS)
[Swift] Data Structure - Graph(BFS)[Swift] Data Structure - Graph(BFS)
[Swift] Data Structure - Graph(BFS)
 
[Swift] Data Structure - Graph(DFS)
[Swift] Data Structure - Graph(DFS)[Swift] Data Structure - Graph(DFS)
[Swift] Data Structure - Graph(DFS)
 
[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Binary Tree[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Binary Tree
 
[Swift] Data Structure - Tree
[Swift] Data Structure - Tree[Swift] Data Structure - Tree
[Swift] Data Structure - Tree
 
[Swift] Data Structure - Graph
[Swift] Data Structure - Graph[Swift] Data Structure - Graph
[Swift] Data Structure - Graph
 

[Swift] Types

  • 2. 목차 •Types? •Named type •Compound Type •Tuple Type •Function Type •Variadic Parameter •References
  • 3. Types? Swift에서의 타입을 크게 보면 2가지로 분류할 수 있습니다. named Type : 명명된 타입 compound Type : 복합(명명되지 않은) 타입 출처: https://noahlogs.tistory.com/13 [인생의 로그캣]
  • 4. Named type 명명된 타입은 클래스, 구조체, 열거형과 프로토콜 등 이름이 있는 타입입니다.
  • 5. Compound Type 복합 타입은 튜플과 함수 등과 같이 이름이 없는 타입입니다
  • 6. Tuple Type 튜플은 기본 Swift내에 명명된 데이터 타입(Named Type)외에도 새롭게 선언한 튜플 타입도 저장할 수도 있습니다. // 튜플 타입 정의 var greatTuple = (x: 10, y: 20) print(greatTuple.0) // 10 print(greatTuple.x) // 10 typealias Point = (Int, Int) // typealias를 활요하여 튜플 타입에 이름을 지정할 수 있습니다. var point = Point(10, 12) print(point.1) // 12
  • 7. Function Type 튜플처럼 함수 타입도 일반적으로 사용할 수 있습니다. 아무 것도 반환하고 싶지 않으면 아무 것도 쓰지 않으면 됩니다. 함수 이름과 중괄호 사이에 있는 것이 함수 타입입니다 func processPerson (withID: Int) -> () {} func processVIP (withID: Int) {} struct Person { var firstname : String var lastname : String } func nameForPerson1 (withID: Int) -> (name: String, age: Int) { return ( "Bill", 20) } func nameForPerson2 (withID: Int) -> (Person) { return (Person(firstname: "Joyce", lastname: “Cai")) } func nameForPerson3 (withID: Int) -> Person { return Person(firstname: "Bob", lastname: "Lee") } func detailsForPerson (withID: Int) -> (obj: Person, age: Int) { return (Person(firstname: "Bill", lastname: "Kim"), 10) }
  • 8. Variadic Parameter Swift 에서도 여타 다른 언어들처럼 아래와 같이 가변 매개 변수 를 지원합니다. // 함수 인자 앞에 ‘_’ 지시어를 붙이면 변수명을 생략할 수 있습니다. func printAll1(_ numbers: Int...) { for number in numbers { print ( "the number (number)") } } printAll1(0, 1, 2, 3, 4) func printAll2( numbers: Int...) { for number in numbers { print ( "the number (number)") } } printAll2(numbers: 0, 1, 2, 3, 4)
  • 9. References [1] Swift ) Types : https://zeddios.tistory.com/207 [2] Swift 타입의 비밀 : https://academy.realm.io/kr/ posts/altconf-2017-manu-rink-secret-life-of-types-in- swift/ [3] Types : https://docs.swift.org/swift-book/ ReferenceManual/Types.html#//apple_ref/swift/ grammar/type