본문 바로가기

전체 글24

\Root.Value 는 (Root) -> Value이다 KeyPath 번역하자면 "특정 루트 유형에서 특정 결과 값 유형으로의 키 경로" 다시 쉽게 이해하자면 특정 루트 유형(내가 알고 싶은 값을 담고있는 타입)에서 특정결괏값 유형(내가 알고 싶은 값)의 경로 class KeyPath class KeyPath 예시 struct Video { let ratio: Ratio var quality: Float } struct Ratio { var horizontal: Int var vertical: Int } var gameVideo = Video( ratio: .init( horizontal: 16, vertical: 9), quality: 720 ) 여기서 Ratio의 값을 접근해야한다면 1. 프로퍼티에 접근한다 2. 함수로 접근한다 3. KeyPath를 사용.. 2023. 9. 26.
Swift에서 Map 만들어보기 고차함수 중 가장 많이 사용하고있는 Map을 직접만들어서 사용하는 것을 구현해보도록 하겠습니다. swift의 맵을 정의하는 곳을 보면 transform이라는 클로져를 통해서 Generic타입으로 새로운 값을 가져올 수 있도록 되어 있습니다. func map(_ transform: (Self.Element) throws -> T) rethrows -> [T] extension Sequence { func customMap(_ transform: (Self.Element) throws -> T) rethrows -> [T] { let new = [T]() //새로운 그릇 do { // 순회하며 transform을 통해서 새로운 값을 전달받고 append try self.forEach{ new.append(t.. 2023. 5. 3.
UICollectionViewCell에서 IndexPath 가져오는 법 UIcollectionViewCell이 자신의 현재 indexPath를 알려면 직접 indexPath를 넣어 줄 수 있습니다. final class TestCell: UICollectionViewCell { //... var indexPath: IndexPah } //UICollectionViewDataSource func collectionView( _ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { //... guardelet cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TestCell") as? TestCel.. 2023. 4. 24.
if else가 많을 때 사용할 수 있는 패턴 개발을 하다 보면 어떠한 조건을 체크하는 경우가 있는데, 조건이 너무 많으면, if 문이 길어질 수 있습니다. if문은 처리하는 속도는 빠르지만 길어질 경우 가독성이 떨어지고, 유지보수가 어려운 점이 있습니다. 아래 예시는 새로운 타입이 추가될 때마다 else if문으로 추가해야 합니다. func checkMediaType(type: String) -> String { var result = "" if type == "Picture" { result = "This media type is Picture" } else if type == "Video" { result = "This media type is Video" } else if type == "Text" { result = "This media t.. 2023. 4. 21.