Swift/기초 카테고리에 업로드한 모든 내용은 KxCoding 기초 강의를 토대로 작성하였습니다.

⎮ filter 메서드
1. Apple Developer에서 filter 레퍼런스 확인
https://developer.apple.com/documentation/swift/string/filter(_:)
filter(_:) | Apple Developer Documentation
Returns a new collection of the same type containing, in order, the elements of the original collection that satisfy the given predicate.
developer.apple.com

⎮ filter 문법 최적화
1. 짝수를 filter하는 코드 작성
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
arr.filter({ (num: Int) -> Bool in
return num.isMultiple(of: 2)
})

2. 문법 최적화
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
var result = arr.filter({ (num: Int) -> Bool in
return num.isMultiple(of: 2)
})
print(result)
| 구 분 | 코 드 | 설 명 |
| 1 | result = arr.filter({ (num) -> Bool in return num.isMultiple(of: 2) }) print(result) |
parameter type은 compiler가 인식할 수 있어서 생략 가능 |
| 2 | result = arr.filter({ (num) in return num.isMultiple(of: 2) }) print(result) |
Return형도 compiler가 인식할 수 있어서 생략 가능 |
| 3 | result = arr.filter({ return num.isMultiple(of: 2) }) print(result) |
compiler는 method 선언을 통해 parameter 자체도 인식할 수 있어서 body에서 parameter name을 사용하고 있다면, parameter 이름 생략 가능 (이때 in도 생략 가능) |
| 4 | result = arr.filter({ return $0.isMultiple(of: 2) }) print(result) |
num이라는 이름으로 더이상 접근 불가, index parameter인 $n를 사용 (short-hand argument name) |
| 5 | result = arr.filter({ $0.isMultiple(of: 2) }) print(result) |
Closure body에 return문 하나 밖에 없으면, return 키워드도 생략 가능 |
| 6 | result = arr.filter({ $0.isMultiple(of: 2) }) print(result) |
코드가 짧으면 이어쓰기 가능 (implicit return) |
| 7 | result = arr.filter() { $0.isMultiple(of: 2) } // inline closure print(result) |
Closure가 유일한 parameter이면서, 마지막 parameter일 때는 잘라내서 () 뒤로 뺄 수 있음 |
| 8 | result = arr.filter { $0.isMultiple(of: 2) } // trailing closure print(result) |
이때는 ()도 생략 가능 |
⎮ 계산기에서 문법 최적화
1. 문법 최적화 전 코드 작성
let actionn = UIAlertAction(title: "+ (더하기)", style: .default, handler: { (action: UIAlertAction) -> Void in
self.operatorButton.setTitle("+", for: .normal)
})

2. 문법 최적화
let actionn = UIAlertAction(title: "+ (더하기)", style: .default, handler: { (action: UIAlertAction) -> Void in
self.operatorButton.setTitle("+", for: .normal)
})
| 구 분 | 코 드 | 설 명 |
| 1 | let action = UIAlertAction(title: "+ (더하기)", style: .default, handler: { (action) -> Void in self.operatorButton.setTitle("+", for: .normal) }) |
parameter type은 compiler가 인식할 수 있어 생략 가능 |
| 2 | let action = UIAlertAction(title: "+ (더하기)", style: .default, handler: { (action) in self.operatorButton.setTitle("+", for: .normal) }) |
Return형도 compiler가 인식할 수 있어 생략 가능 |
| 3 | let action = UIAlertAction(title: "+ (더하기)", style: .default, handler: { (action) in self.operatorButton.setTitle("+", for: .normal) }) |
* 원래는 이름을 생략해야하는데, 여기서 action을 생략하면 에러 발생. self.operatorButton 이하에서 parameter에 접근하지 않으니까 short-hand argument name 불가 |
| 4 | let action = UIAlertAction(title: "+ (더하기)", style: .default, handler: { _ in self.operatorButton.setTitle("+", for: .normal) }) |
body에서 parameter를 사용하지 않으면, 완전히 생략할 수는 없기 때문에, _ 문자로 생략 이때, in 키워드는 반드시 써야함 |
| 5 | let action = UIAlertAction(title: "+ (더하기)", style: .default, handler: ) { _ in self.operatorButton.setTitle("+", for: .normal) } |
{}를 괄호 뒤로 뺄 수 있음. |
| 6 | let action = UIAlertAction(title: "+ (더하기)", style: .default) { _ in self.operatorButton.setTitle("+", for: .normal) } |
남아 있는 Argument label 삭제 |

⎮ contains 메서드
1. Apple Developer에서 contains 레퍼런스 확인
https://developer.apple.com/documentation/swift/array/contains(where:)
contains(where:) | Apple Developer Documentation
Returns a Boolean value indicating whether the sequence contains an element that satisfies the given predicate.
developer.apple.com

⎮ contains 문법 최적화
1. 문법 최적화
let cast1 = ["Vivien", "Marlon", "Kim", "Karl"]
cast1.contains(where: { (name: String) -> Bool in
return name.hasPrefix("V")
})
| 구 분 | 코 드 | 설 명 |
| 1 | cast1.contains(where: { (name) -> Bool in return name.hasPrefix("V") }) |
parameter type은 compiler가 인식할 수 있어서 생략 가능 |
| 2 | cast1.contains(where: { (name) in return name.hasPrefix("V") }) |
Return형도 compiler가 인식할 수 있어서 생략 가능 |
| 3 | cast1.contains(where: { return name.hasPrefix("V") }) |
compiler는 method 선언을 통해 parameter 자체도 인식할 수 있어서 body에서 parameter name을 사용하고 있다면, parameter 이름 생략 가능 (이때 in도 생략 가능) |
| 4 | cast1.contains(where: { return $0.hasPrefix("V") }) |
name이라는 이름으로 더이상 접근 불가, index parameter인 $n를 사용 (short-hand argument name) |
| 5 | cast1.contains(where: { $0.hasPrefix("V") }) |
Closure body에 return문 하나 밖에 없으면, return 키워드도 생략 가능 |
| 6 | cast1.contains(where: { $0.hasPrefix("V") }) | 코드가 짧으면 이어쓰기 가능 (implicit return) |
| 7 | cast1.contains(where: ) { $0.hasPrefix("V") } | {}를 맨 뒤로 빼기 |
| 8 | cast1.contains { $0.hasPrefix("V") } | ()만 남았거나 argument Label만 남아 있다면, 괄호를 지움 |
'Swift > 기초' 카테고리의 다른 글
| 32. 스위프트 기초 문법[Type(1) - Class & Structure] (0) | 2025.02.07 |
|---|---|
| 31. 스위프트 기초 문법[계산기(10) - 함수 표기법, View Animation] (0) | 2025.02.06 |
| 29. 스위프트 기초 문법[계산기(8) - 코드 분석] (0) | 2025.02.04 |
| 28. 스위프트 기초 문법[계산기(7) - Closure] (0) | 2025.02.03 |
| 27. 스위프트 기초 문법[로또(6) - 정렬과 회전] (1) | 2025.02.02 |