SlideShare a Scribd company logo
1 of 16
Download to read offline
SWIFT
Type Inference
Bill Kim(김정훈) | ibillkim@gmail.com
목차
•Inference
•Type Inference
•Inference with Closure
•References
Inference
추론은 이미 알고 있는 또는 확인된 정보로부터 논리적 결론을 도출
하는 행위 또는 과정이다.
따라서 추론은 '어떠한 판단을 근거로 삼아 다른 판단을 이끌어 내
는 것’이라고 할 수 있다
Type Inference
Swift에서의 타입 추론은 애플에서도 Swift의 강력한 기능이라고
소개하고 있다.
타입 추론은 변수나 상수를 생성할 때 데이터 타입을 생략하면 컴파
일러가 변수 값의 타입을 추론하여 타입을 지정한다.
Type Inference
let name1: String = “Bill Kim” // String 타입으로 상수 선언
let name2 = “Joyce” // 타입 추론을 통한 자동으로 타입 지정
var age1: Int = 20 // Int 타입으로 변수 지정
var age2 = 18 // 타입 추론을 통하여 Int 타입 지정
var weight1: Float = 50.5 // Float 타입으로 변수 지정
var weight2 = 65.2 // 타입 추론을 통하여 Float 타입 지정
Inference with Closure
Closure(클로저)란?
Closure란 일회용 함수를 작성할 수 있는 코드 블럭을 의미
Closure의 대한 자세한 문법은 다른 별도의 강의에서 전달
본 강의는 Closure에서의 추론이 사용되는 예시에 대해서만 소개
해드리겠습니다.
Inference with Closure
클로저 축약방식
- 매개변수 타입과 리턴타입은 추론 가능하므로 축약 가능
- 리턴이라는 지시어도 축약 가능
- 매개변수의 이름은 내부적으로 사용하는 것이므로 축약하고 위치
로 참조 가능하므로 축약한다
Inference with Closure
매개변수 타입 추론
let numbers = [1, 4, 56, 22, 5]
// 일반적인 Closure 예시
let sort = numbers.sorted(by: { (i: Int, j: Int) -> Bool in
return i < j
})
// 매개변수 타입 추론 예시
let sort = numbers.sorted(by: { (i, j) -> Bool in
return i < j
})
Inference with Closure
리턴 타입 추론
// 리턴 타입 추론 예시
let sort = numbers.sorted(by: { (i, j) in
return i < j
})
Inference with Closure
리턴 축약
// 리턴 축약 예시
let sort = numbers.sorted(by: { (i, j) in i < j })
Inference with Closure
매개변수 이름 추론
// 축약형 인수형(매개변수 이름) 사용 예시
let sort = numbers.sorted(by: { $0 < $1 })
축약형 인수형을 통해서는 Closure 문법에서의 in도 생략할 수 있다.
Inference with Closure
후행 클로저 사용
// 후행 클로저를 통하여 {} 지시자를 빼는 것이 가능
let sort = numbers.sorted() { $0 < $1 }
Inference with Closure
소괄호 생략
// 메서드의 소괄호까지 생략 가능
let sort = numbers.sorted { $0 < $1 }
References
[1] 추론(위키백과) : https://ko.wikipedia.org/wiki/추론
[2] Swift3 타입 추론(Type Infeence) : https://
baboototo.tistory.com/12
[3] Swift 기초문법 – 변수, 상수, 기초 데이터 타입 : https://
blog.yagom.net/527/
[4] Closure 축약하기 : https://programmers.co.kr/learn/
courses/4/lessons/560
[5] Swift 클로저(Closure) : https://nightohl.tistory.com/
entry/Swift-클로저Closure
References
[6] Swift - 클로저 & 고차함수 : https://medium.com/
@ggaa96/swift-study-1-클로저-고차함수-abe199f22ad8
Thank you!

More Related Content

What's hot

외계어 스터디 3/5 function and object
외계어 스터디 3/5   function and object외계어 스터디 3/5   function and object
외계어 스터디 3/5 function and object민태 김
 
Swift 3 Programming for iOS : data type
Swift 3 Programming for iOS : data typeSwift 3 Programming for iOS : data type
Swift 3 Programming for iOS : data typeKwang Woo NAM
 
비개발자를 위한 Javascript 알아가기 #6
비개발자를 위한 Javascript 알아가기 #6비개발자를 위한 Javascript 알아가기 #6
비개발자를 위한 Javascript 알아가기 #6민태 김
 
Effective c++ Chapter1,2
Effective c++ Chapter1,2Effective c++ Chapter1,2
Effective c++ Chapter1,2문익 장
 
Basic study 2회차
Basic study 2회차Basic study 2회차
Basic study 2회차Seonmun Choi
 
생코자바스크립트스터디3장
생코자바스크립트스터디3장생코자바스크립트스터디3장
생코자바스크립트스터디3장Jinhwa Hong
 
외계어 스터디 2/5 - Expressions & statements
외계어 스터디 2/5 - Expressions & statements외계어 스터디 2/5 - Expressions & statements
외계어 스터디 2/5 - Expressions & statements민태 김
 
Start IoT with JavaScript - 1.기초
Start IoT with JavaScript - 1.기초Start IoT with JavaScript - 1.기초
Start IoT with JavaScript - 1.기초Park Jonggun
 
More effective c++ 항목30부터
More effective c++ 항목30부터More effective c++ 항목30부터
More effective c++ 항목30부터Dong Chan Shin
 
Effective c++ chapter1 2_dcshin
Effective c++ chapter1 2_dcshinEffective c++ chapter1 2_dcshin
Effective c++ chapter1 2_dcshinDong Chan Shin
 
이펙티브 C++ 공부
이펙티브 C++ 공부이펙티브 C++ 공부
이펙티브 C++ 공부quxn6
 
More effective c++ chapter1 2_dcshin
More effective c++ chapter1 2_dcshinMore effective c++ chapter1 2_dcshin
More effective c++ chapter1 2_dcshinDong Chan Shin
 
비개발자를 위한 Javascript 알아가기 #5.1
비개발자를 위한 Javascript 알아가기 #5.1비개발자를 위한 Javascript 알아가기 #5.1
비개발자를 위한 Javascript 알아가기 #5.1민태 김
 
Reflect package 사용하기
Reflect package 사용하기Reflect package 사용하기
Reflect package 사용하기Yong Joon Moon
 
9 object class
9 object class9 object class
9 object class웅식 전
 
Effective c++ chapter3, 4 요약본
Effective c++ chapter3, 4 요약본Effective c++ chapter3, 4 요약본
Effective c++ chapter3, 4 요약본Dong Chan Shin
 

What's hot (20)

외계어 스터디 3/5 function and object
외계어 스터디 3/5   function and object외계어 스터디 3/5   function and object
외계어 스터디 3/5 function and object
 
07. type system
07. type system07. type system
07. type system
 
01. basic types
01. basic types01. basic types
01. basic types
 
Swift 3 Programming for iOS : data type
Swift 3 Programming for iOS : data typeSwift 3 Programming for iOS : data type
Swift 3 Programming for iOS : data type
 
비개발자를 위한 Javascript 알아가기 #6
비개발자를 위한 Javascript 알아가기 #6비개발자를 위한 Javascript 알아가기 #6
비개발자를 위한 Javascript 알아가기 #6
 
Effective c++ Chapter1,2
Effective c++ Chapter1,2Effective c++ Chapter1,2
Effective c++ Chapter1,2
 
Basic study 2회차
Basic study 2회차Basic study 2회차
Basic study 2회차
 
Java standard(8~13)
Java standard(8~13)Java standard(8~13)
Java standard(8~13)
 
생코자바스크립트스터디3장
생코자바스크립트스터디3장생코자바스크립트스터디3장
생코자바스크립트스터디3장
 
외계어 스터디 2/5 - Expressions & statements
외계어 스터디 2/5 - Expressions & statements외계어 스터디 2/5 - Expressions & statements
외계어 스터디 2/5 - Expressions & statements
 
Start IoT with JavaScript - 1.기초
Start IoT with JavaScript - 1.기초Start IoT with JavaScript - 1.기초
Start IoT with JavaScript - 1.기초
 
More effective c++ 항목30부터
More effective c++ 항목30부터More effective c++ 항목30부터
More effective c++ 항목30부터
 
Effective c++ chapter1 2_dcshin
Effective c++ chapter1 2_dcshinEffective c++ chapter1 2_dcshin
Effective c++ chapter1 2_dcshin
 
이펙티브 C++ 공부
이펙티브 C++ 공부이펙티브 C++ 공부
이펙티브 C++ 공부
 
More effective c++ chapter1 2_dcshin
More effective c++ chapter1 2_dcshinMore effective c++ chapter1 2_dcshin
More effective c++ chapter1 2_dcshin
 
비개발자를 위한 Javascript 알아가기 #5.1
비개발자를 위한 Javascript 알아가기 #5.1비개발자를 위한 Javascript 알아가기 #5.1
비개발자를 위한 Javascript 알아가기 #5.1
 
9. pointer
9. pointer9. pointer
9. pointer
 
Reflect package 사용하기
Reflect package 사용하기Reflect package 사용하기
Reflect package 사용하기
 
9 object class
9 object class9 object class
9 object class
 
Effective c++ chapter3, 4 요약본
Effective c++ chapter3, 4 요약본Effective c++ chapter3, 4 요약본
Effective c++ chapter3, 4 요약본
 

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] Type inference

  • 3. Inference 추론은 이미 알고 있는 또는 확인된 정보로부터 논리적 결론을 도출 하는 행위 또는 과정이다. 따라서 추론은 '어떠한 판단을 근거로 삼아 다른 판단을 이끌어 내 는 것’이라고 할 수 있다
  • 4. Type Inference Swift에서의 타입 추론은 애플에서도 Swift의 강력한 기능이라고 소개하고 있다. 타입 추론은 변수나 상수를 생성할 때 데이터 타입을 생략하면 컴파 일러가 변수 값의 타입을 추론하여 타입을 지정한다.
  • 5. Type Inference let name1: String = “Bill Kim” // String 타입으로 상수 선언 let name2 = “Joyce” // 타입 추론을 통한 자동으로 타입 지정 var age1: Int = 20 // Int 타입으로 변수 지정 var age2 = 18 // 타입 추론을 통하여 Int 타입 지정 var weight1: Float = 50.5 // Float 타입으로 변수 지정 var weight2 = 65.2 // 타입 추론을 통하여 Float 타입 지정
  • 6. Inference with Closure Closure(클로저)란? Closure란 일회용 함수를 작성할 수 있는 코드 블럭을 의미 Closure의 대한 자세한 문법은 다른 별도의 강의에서 전달 본 강의는 Closure에서의 추론이 사용되는 예시에 대해서만 소개 해드리겠습니다.
  • 7. Inference with Closure 클로저 축약방식 - 매개변수 타입과 리턴타입은 추론 가능하므로 축약 가능 - 리턴이라는 지시어도 축약 가능 - 매개변수의 이름은 내부적으로 사용하는 것이므로 축약하고 위치 로 참조 가능하므로 축약한다
  • 8. Inference with Closure 매개변수 타입 추론 let numbers = [1, 4, 56, 22, 5] // 일반적인 Closure 예시 let sort = numbers.sorted(by: { (i: Int, j: Int) -> Bool in return i < j }) // 매개변수 타입 추론 예시 let sort = numbers.sorted(by: { (i, j) -> Bool in return i < j })
  • 9. Inference with Closure 리턴 타입 추론 // 리턴 타입 추론 예시 let sort = numbers.sorted(by: { (i, j) in return i < j })
  • 10. Inference with Closure 리턴 축약 // 리턴 축약 예시 let sort = numbers.sorted(by: { (i, j) in i < j })
  • 11. Inference with Closure 매개변수 이름 추론 // 축약형 인수형(매개변수 이름) 사용 예시 let sort = numbers.sorted(by: { $0 < $1 }) 축약형 인수형을 통해서는 Closure 문법에서의 in도 생략할 수 있다.
  • 12. Inference with Closure 후행 클로저 사용 // 후행 클로저를 통하여 {} 지시자를 빼는 것이 가능 let sort = numbers.sorted() { $0 < $1 }
  • 13. Inference with Closure 소괄호 생략 // 메서드의 소괄호까지 생략 가능 let sort = numbers.sorted { $0 < $1 }
  • 14. References [1] 추론(위키백과) : https://ko.wikipedia.org/wiki/추론 [2] Swift3 타입 추론(Type Infeence) : https:// baboototo.tistory.com/12 [3] Swift 기초문법 – 변수, 상수, 기초 데이터 타입 : https:// blog.yagom.net/527/ [4] Closure 축약하기 : https://programmers.co.kr/learn/ courses/4/lessons/560 [5] Swift 클로저(Closure) : https://nightohl.tistory.com/ entry/Swift-클로저Closure
  • 15. References [6] Swift - 클로저 & 고차함수 : https://medium.com/ @ggaa96/swift-study-1-클로저-고차함수-abe199f22ad8