SlideShare a Scribd company logo
1 of 15
Download to read offline
SWIFT
Methods
Bill Kim(김정훈) | ibillkim@gmail.com
목차
•Method
•Instance Method
•The self Property
•mutating
•Type Methods
•References
Method
메소드 : 특정 타입의 클래스, 구조체, 열거형과 관련된 함수
1) 인스턴스 메소드 : 특정 인스턴스에서 실행할 수 있는 메소드
2) 타입 메소드 : 특정 형과 관련된 메소드(클래스 메소드와 유사)
Objective C에서는 클래스 타입에서만 메소드를 선언할 수 있지
만 Swift에서는 구조체, 열거형에서도 메소드를 선언할 수 있다.
Instance Method
인스턴스 메소드 : 특정 클래스, 구조체, 열거형의 인스턴스에 속한
메소드를 지칭
class Counter {
var count = 0
// Instance Method
func increment() {
count += 1
}
}
let counter = Counter()
counter.increment()
print(counter.count) // 1
The self Property
모드 프로퍼티는 암시적으로 인스턴스 자체를 의미하는 self 라는
프로퍼티를 가지고 있습니다.
인스턴스 메소드 안에서 self 프로퍼티를 이용하여 인스턴스 자체
를 참조하는데 사용할 수 있습니다.
class Counter {
var count = 0
// Instance Method
func increment() {
self.count += 1 // self 프로퍼티를 이용해 인스턴스 자체를 참조
}
}
mutating
기본적으로 구조체와 열거형은 값 타입(Value Type)입니다.
따라서 인스턴스 메소드 내에서는 값 타입의 프로퍼티를 변경할 수
없습니다.
아래의 코드에서는 다음과 같이 컴파일 에러가 발생합니다.
mutating
따라서 값 타입 형의 메소드에서 프로퍼티를 변경하고 싶을 경우에
는 mutating 이라는 키워드를 사용하여 프로퍼티를 변경할 수 있
습니다.
struct Point {
var x = 0.0, y = 0.0
mutating func moveBy(x deltaX: Double, y deltaY: Double) {
self.x += deltaX
self.y += deltaY
}
}
var somePoint = Point(x: 1.0, y: 1.0)
somePoint.moveBy(x: 2.0, y: 3.0)
print("The point is now at ((somePoint.x), (somePoint.y))”)
mutating
mutating 메소드 안에서 self 프로퍼티를 사용하여 완전히 새로
운 인스턴스를 생성할 수 있습니다.
struct Point {
var x = 0.0, y = 0.0
mutating func moveBy(x deltaX: Double, y deltaY: Double) {
// self를 통하여 새로운 자신의 Point 인스턴스를 생성하여 값을 설정할 수 있습니다.
// self.x += deltaX
// self.y += deltaY
self = Point(x: x + deltaX, y: y + deltaY)
}
}
mutating
열거형(enum)에서 mutating 메소드 사용하여 다른 상태로의 전환 등을 표현
할 수 있습니다.
enum TriStateSwitch {
case off, low, high
mutating func next() {
switch self {
case .off:
self = .low
case .low:
self = .high
case .high:
self = .off
}
}
}
var ovenLight = TriStateSwitch.low
ovenLight.next()
// ovenLight 값은 .high
print(ovenLight)
ovenLight.next()
// ovenLight 값은 .off
print(ovenLight)
Type Methods
타입 메소드 : 특정 타입 자체에서 호출하여 사용하는 함수를 지칭
func 키워드 앞에 static 또는 class 키워드를 추가하여 사용
1) static 메소드 : 서브클래스에서 오버라이드를 할 수 없음
2) class 메소드 : 서브클래스에서 오버라이드가 가능
기존 Objective C는 클래스에서만 타입 메소드를 선언할 수 있던
것과 달리 Swift는 클래스, 구조체, 열거형에서 모두 타입 메소드를
사용할 수 있습니다.
Type Methods
class SomeClass {
var value:Int = 0 // 타입 메소드 안에서 사용 불가
static var valueStatic:Int = 0
// 타입 메소드
class func someTypeMethod() {
value = 10 // 컴파일 에러
self.value = 1 // 컴파일 에러
// 타입 메소드 안에서의 self는 인스턴스가 아닌 타입 자신을 의미
self.valueStatic = 1 // static으로 선언하여 사용 가능
}
}
SomeClass.someTypeMethod() // 타입 메소드 호출!
Type Methods
타입 메소드에서 @discardableResult 키워드를 사용하면 리턴
값이 있는 메소드를 호출 후 리턴값을 사용하지 않을 경우 컴파일
경고가 발생하는데 그 경고가 발생하지 않도록 해줍니다.
struct LevelTracker {
var currentLevel = 1
@discardableResult
mutating func advance(to level: Int) -> Bool {
if currentLevel > 10 {
return true
} else {
return false
}
}
}
만약 @discardableResult 키워드를 붙이지 않을 경우 컴파일 시
아래와 같은 경고 메시지가 표시됩니다.
References
[1] [Swift]Methods 정리 : http://minsone.github.io/mac/
ios/swift-methods-summary
[2] Methods : https://docs.swift.org/swift-book/
LanguageGuide/Methods.html
[3] Swift ) Method : https://zeddios.tistory.com/258
[4] [Swift 3] 메소드 (Method) : https://
beankhan.tistory.com/162
[5] Swift - 메소드(Method) : http://seorenn.blogspot.com/
2014/06/swift-method.html
References
[6] Swift의 static 메서드와 class 메서드 : https://
medium.com/@miles3898/swift의-static-메서드와-class-메
서드-975d367c4c19
[7] Swift - Methods : https://www.tutorialspoint.com/
swift/swift_methods.htm
[8] Swift4 :메소드 : Method : #메소드의 범위 : #mutating :
#self #값 타입 수정 : https://the-brain-of-sic2.tistory.com/7
[9] Methods : https://wlaxhrl.tistory.com/44
[10] [Swift 5.2] Methods - Apple Documentation : https://
you9010.tistory.com/298
Thank you!

More Related Content

What's hot

이펙티브 C++ 스터디
이펙티브 C++ 스터디이펙티브 C++ 스터디
이펙티브 C++ 스터디quxn6
 
Effective c++ 1
Effective c++ 1Effective c++ 1
Effective c++ 1현찬 양
 
이펙티브 C++ (7~9)
이펙티브 C++ (7~9)이펙티브 C++ (7~9)
이펙티브 C++ (7~9)익성 조
 
Effective c++ 4
Effective c++ 4Effective c++ 4
Effective c++ 4현찬 양
 
More effective c++ 2
More effective c++ 2More effective c++ 2
More effective c++ 2현찬 양
 
Effective c++ chapter 1,2 요약
Effective c++ chapter 1,2 요약Effective c++ chapter 1,2 요약
Effective c++ chapter 1,2 요약Nam Hyeonuk
 
More effective c++ 3
More effective c++ 3More effective c++ 3
More effective c++ 3현찬 양
 
Tcpl 14장 예외처리
Tcpl 14장 예외처리Tcpl 14장 예외처리
Tcpl 14장 예외처리재정 이
 
Swift세미나(속성(properties), 메소드(method))
Swift세미나(속성(properties), 메소드(method))Swift세미나(속성(properties), 메소드(method))
Swift세미나(속성(properties), 메소드(method))경원 정
 
이펙티브 C++ 5,6 장 스터디
이펙티브 C++ 5,6 장 스터디이펙티브 C++ 5,6 장 스터디
이펙티브 C++ 5,6 장 스터디quxn6
 
Scala block expression
Scala block expressionScala block expression
Scala block expressionYong Joon Moon
 
Scala self type inheritance
Scala self type inheritanceScala self type inheritance
Scala self type inheritanceYong Joon Moon
 
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
 
Effective C++ Chapter 2 Summary
Effective C++ Chapter 2 SummaryEffective C++ Chapter 2 Summary
Effective C++ Chapter 2 SummarySeungYeonChoi10
 
Start IoT with JavaScript - 7.프로토타입
Start IoT with JavaScript - 7.프로토타입Start IoT with JavaScript - 7.프로토타입
Start IoT with JavaScript - 7.프로토타입Park Jonggun
 
Effective c++ 정리 1~2
Effective c++ 정리 1~2Effective c++ 정리 1~2
Effective c++ 정리 1~2Injae Lee
 
Effective C++ Chaper 1
Effective C++ Chaper 1Effective C++ Chaper 1
Effective C++ Chaper 1연우 김
 
스칼라 클래스 이해하기 _Scala class understanding
스칼라 클래스 이해하기 _Scala class understanding스칼라 클래스 이해하기 _Scala class understanding
스칼라 클래스 이해하기 _Scala class understandingYong Joon Moon
 
비개발자를 위한 Javascript 알아가기 #6
비개발자를 위한 Javascript 알아가기 #6비개발자를 위한 Javascript 알아가기 #6
비개발자를 위한 Javascript 알아가기 #6민태 김
 

What's hot (20)

이펙티브 C++ 스터디
이펙티브 C++ 스터디이펙티브 C++ 스터디
이펙티브 C++ 스터디
 
Effective c++ 1
Effective c++ 1Effective c++ 1
Effective c++ 1
 
이펙티브 C++ (7~9)
이펙티브 C++ (7~9)이펙티브 C++ (7~9)
이펙티브 C++ (7~9)
 
Effective c++ 4
Effective c++ 4Effective c++ 4
Effective c++ 4
 
More effective c++ 2
More effective c++ 2More effective c++ 2
More effective c++ 2
 
Effective c++ chapter 1,2 요약
Effective c++ chapter 1,2 요약Effective c++ chapter 1,2 요약
Effective c++ chapter 1,2 요약
 
More effective c++ 3
More effective c++ 3More effective c++ 3
More effective c++ 3
 
Tcpl 14장 예외처리
Tcpl 14장 예외처리Tcpl 14장 예외처리
Tcpl 14장 예외처리
 
Swift세미나(속성(properties), 메소드(method))
Swift세미나(속성(properties), 메소드(method))Swift세미나(속성(properties), 메소드(method))
Swift세미나(속성(properties), 메소드(method))
 
이펙티브 C++ 5,6 장 스터디
이펙티브 C++ 5,6 장 스터디이펙티브 C++ 5,6 장 스터디
이펙티브 C++ 5,6 장 스터디
 
Scala block expression
Scala block expressionScala block expression
Scala block expression
 
Scala self type inheritance
Scala self type inheritanceScala self type inheritance
Scala self type inheritance
 
More effective c++ chapter1 2_dcshin
More effective c++ chapter1 2_dcshinMore effective c++ chapter1 2_dcshin
More effective c++ chapter1 2_dcshin
 
Effective C++ Chapter 2 Summary
Effective C++ Chapter 2 SummaryEffective C++ Chapter 2 Summary
Effective C++ Chapter 2 Summary
 
Scala variable
Scala variableScala variable
Scala variable
 
Start IoT with JavaScript - 7.프로토타입
Start IoT with JavaScript - 7.프로토타입Start IoT with JavaScript - 7.프로토타입
Start IoT with JavaScript - 7.프로토타입
 
Effective c++ 정리 1~2
Effective c++ 정리 1~2Effective c++ 정리 1~2
Effective c++ 정리 1~2
 
Effective C++ Chaper 1
Effective C++ Chaper 1Effective C++ Chaper 1
Effective C++ Chaper 1
 
스칼라 클래스 이해하기 _Scala class understanding
스칼라 클래스 이해하기 _Scala class understanding스칼라 클래스 이해하기 _Scala class understanding
스칼라 클래스 이해하기 _Scala class understanding
 
비개발자를 위한 Javascript 알아가기 #6
비개발자를 위한 Javascript 알아가기 #6비개발자를 위한 Javascript 알아가기 #6
비개발자를 위한 Javascript 알아가기 #6
 

Similar to [Swift] Methods

Swift5 vs objective c
Swift5 vs objective cSwift5 vs objective c
Swift5 vs objective cBill Kim
 
Scala type class pattern
Scala type class patternScala type class pattern
Scala type class patternYong Joon Moon
 
[Swift] Access control
[Swift] Access control[Swift] Access control
[Swift] Access controlBill Kim
 
파이썬+클래스+구조+이해하기 20160310
파이썬+클래스+구조+이해하기 20160310파이썬+클래스+구조+이해하기 20160310
파이썬+클래스+구조+이해하기 20160310Yong Joon Moon
 
파이썬+객체지향+이해하기 20160131
파이썬+객체지향+이해하기 20160131파이썬+객체지향+이해하기 20160131
파이썬+객체지향+이해하기 20160131Yong Joon Moon
 
Data Structure 4
Data Structure 4Data Structure 4
Data Structure 4yonsei
 
Android Programming
Android ProgrammingAndroid Programming
Android ProgrammingJake Yoon
 
Android Programming - Input
Android Programming - InputAndroid Programming - Input
Android Programming - InputJake Yoon
 
[Swift] Extensions
[Swift] Extensions[Swift] Extensions
[Swift] ExtensionsBill Kim
 
Effective c++chapter4
Effective c++chapter4Effective c++chapter4
Effective c++chapter4성연 김
 
C++ struct copy
C++ struct copyC++ struct copy
C++ struct copy송미 이
 
2014-15 Intermediate C++ Study #7
2014-15 Intermediate C++ Study #72014-15 Intermediate C++ Study #7
2014-15 Intermediate C++ Study #7Chris Ohk
 
[아꿈사] The C++ Programming Language 13장 템플릿
[아꿈사] The C++ Programming Language 13장 템플릿[아꿈사] The C++ Programming Language 13장 템플릿
[아꿈사] The C++ Programming Language 13장 템플릿해강
 
Use JavaScript more strictly (feat. TypeScript, flow)
Use JavaScript more strictly (feat. TypeScript, flow)Use JavaScript more strictly (feat. TypeScript, flow)
Use JavaScript more strictly (feat. TypeScript, flow)Mark Lee
 

Similar to [Swift] Methods (15)

Swift5 vs objective c
Swift5 vs objective cSwift5 vs objective c
Swift5 vs objective c
 
Scala type class pattern
Scala type class patternScala type class pattern
Scala type class pattern
 
[Swift] Access control
[Swift] Access control[Swift] Access control
[Swift] Access control
 
파이썬+클래스+구조+이해하기 20160310
파이썬+클래스+구조+이해하기 20160310파이썬+클래스+구조+이해하기 20160310
파이썬+클래스+구조+이해하기 20160310
 
Haskell study 6
Haskell study 6Haskell study 6
Haskell study 6
 
파이썬+객체지향+이해하기 20160131
파이썬+객체지향+이해하기 20160131파이썬+객체지향+이해하기 20160131
파이썬+객체지향+이해하기 20160131
 
Data Structure 4
Data Structure 4Data Structure 4
Data Structure 4
 
Android Programming
Android ProgrammingAndroid Programming
Android Programming
 
Android Programming - Input
Android Programming - InputAndroid Programming - Input
Android Programming - Input
 
[Swift] Extensions
[Swift] Extensions[Swift] Extensions
[Swift] Extensions
 
Effective c++chapter4
Effective c++chapter4Effective c++chapter4
Effective c++chapter4
 
C++ struct copy
C++ struct copyC++ struct copy
C++ struct copy
 
2014-15 Intermediate C++ Study #7
2014-15 Intermediate C++ Study #72014-15 Intermediate C++ Study #7
2014-15 Intermediate C++ Study #7
 
[아꿈사] The C++ Programming Language 13장 템플릿
[아꿈사] The C++ Programming Language 13장 템플릿[아꿈사] The C++ Programming Language 13장 템플릿
[아꿈사] The C++ Programming Language 13장 템플릿
 
Use JavaScript more strictly (feat. TypeScript, flow)
Use JavaScript more strictly (feat. TypeScript, flow)Use JavaScript more strictly (feat. TypeScript, flow)
Use JavaScript more strictly (feat. TypeScript, flow)
 

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] Methods

  • 2. 목차 •Method •Instance Method •The self Property •mutating •Type Methods •References
  • 3. Method 메소드 : 특정 타입의 클래스, 구조체, 열거형과 관련된 함수 1) 인스턴스 메소드 : 특정 인스턴스에서 실행할 수 있는 메소드 2) 타입 메소드 : 특정 형과 관련된 메소드(클래스 메소드와 유사) Objective C에서는 클래스 타입에서만 메소드를 선언할 수 있지 만 Swift에서는 구조체, 열거형에서도 메소드를 선언할 수 있다.
  • 4. Instance Method 인스턴스 메소드 : 특정 클래스, 구조체, 열거형의 인스턴스에 속한 메소드를 지칭 class Counter { var count = 0 // Instance Method func increment() { count += 1 } } let counter = Counter() counter.increment() print(counter.count) // 1
  • 5. The self Property 모드 프로퍼티는 암시적으로 인스턴스 자체를 의미하는 self 라는 프로퍼티를 가지고 있습니다. 인스턴스 메소드 안에서 self 프로퍼티를 이용하여 인스턴스 자체 를 참조하는데 사용할 수 있습니다. class Counter { var count = 0 // Instance Method func increment() { self.count += 1 // self 프로퍼티를 이용해 인스턴스 자체를 참조 } }
  • 6. mutating 기본적으로 구조체와 열거형은 값 타입(Value Type)입니다. 따라서 인스턴스 메소드 내에서는 값 타입의 프로퍼티를 변경할 수 없습니다. 아래의 코드에서는 다음과 같이 컴파일 에러가 발생합니다.
  • 7. mutating 따라서 값 타입 형의 메소드에서 프로퍼티를 변경하고 싶을 경우에 는 mutating 이라는 키워드를 사용하여 프로퍼티를 변경할 수 있 습니다. struct Point { var x = 0.0, y = 0.0 mutating func moveBy(x deltaX: Double, y deltaY: Double) { self.x += deltaX self.y += deltaY } } var somePoint = Point(x: 1.0, y: 1.0) somePoint.moveBy(x: 2.0, y: 3.0) print("The point is now at ((somePoint.x), (somePoint.y))”)
  • 8. mutating mutating 메소드 안에서 self 프로퍼티를 사용하여 완전히 새로 운 인스턴스를 생성할 수 있습니다. struct Point { var x = 0.0, y = 0.0 mutating func moveBy(x deltaX: Double, y deltaY: Double) { // self를 통하여 새로운 자신의 Point 인스턴스를 생성하여 값을 설정할 수 있습니다. // self.x += deltaX // self.y += deltaY self = Point(x: x + deltaX, y: y + deltaY) } }
  • 9. mutating 열거형(enum)에서 mutating 메소드 사용하여 다른 상태로의 전환 등을 표현 할 수 있습니다. enum TriStateSwitch { case off, low, high mutating func next() { switch self { case .off: self = .low case .low: self = .high case .high: self = .off } } } var ovenLight = TriStateSwitch.low ovenLight.next() // ovenLight 값은 .high print(ovenLight) ovenLight.next() // ovenLight 값은 .off print(ovenLight)
  • 10. Type Methods 타입 메소드 : 특정 타입 자체에서 호출하여 사용하는 함수를 지칭 func 키워드 앞에 static 또는 class 키워드를 추가하여 사용 1) static 메소드 : 서브클래스에서 오버라이드를 할 수 없음 2) class 메소드 : 서브클래스에서 오버라이드가 가능 기존 Objective C는 클래스에서만 타입 메소드를 선언할 수 있던 것과 달리 Swift는 클래스, 구조체, 열거형에서 모두 타입 메소드를 사용할 수 있습니다.
  • 11. Type Methods class SomeClass { var value:Int = 0 // 타입 메소드 안에서 사용 불가 static var valueStatic:Int = 0 // 타입 메소드 class func someTypeMethod() { value = 10 // 컴파일 에러 self.value = 1 // 컴파일 에러 // 타입 메소드 안에서의 self는 인스턴스가 아닌 타입 자신을 의미 self.valueStatic = 1 // static으로 선언하여 사용 가능 } } SomeClass.someTypeMethod() // 타입 메소드 호출!
  • 12. Type Methods 타입 메소드에서 @discardableResult 키워드를 사용하면 리턴 값이 있는 메소드를 호출 후 리턴값을 사용하지 않을 경우 컴파일 경고가 발생하는데 그 경고가 발생하지 않도록 해줍니다. struct LevelTracker { var currentLevel = 1 @discardableResult mutating func advance(to level: Int) -> Bool { if currentLevel > 10 { return true } else { return false } } } 만약 @discardableResult 키워드를 붙이지 않을 경우 컴파일 시 아래와 같은 경고 메시지가 표시됩니다.
  • 13. References [1] [Swift]Methods 정리 : http://minsone.github.io/mac/ ios/swift-methods-summary [2] Methods : https://docs.swift.org/swift-book/ LanguageGuide/Methods.html [3] Swift ) Method : https://zeddios.tistory.com/258 [4] [Swift 3] 메소드 (Method) : https:// beankhan.tistory.com/162 [5] Swift - 메소드(Method) : http://seorenn.blogspot.com/ 2014/06/swift-method.html
  • 14. References [6] Swift의 static 메서드와 class 메서드 : https:// medium.com/@miles3898/swift의-static-메서드와-class-메 서드-975d367c4c19 [7] Swift - Methods : https://www.tutorialspoint.com/ swift/swift_methods.htm [8] Swift4 :메소드 : Method : #메소드의 범위 : #mutating : #self #값 타입 수정 : https://the-brain-of-sic2.tistory.com/7 [9] Methods : https://wlaxhrl.tistory.com/44 [10] [Swift 5.2] Methods - Apple Documentation : https:// you9010.tistory.com/298