99: SnowSeeker, part 4

그루두·2024년 9월 6일
0

100 days of SwiftUI

목록 보기
107/108

Project 19, part 4

퀴즈 기록

  • One static let property can reference another in the same type. Be careful not to make a reference chain by accident, because it can cause your code to crash.
  • Images built from SF Symbols icons have a customizable foreground style. This lets us display the icons in whatever style we want.
  • If we need bindings to an @Observable class from - the environment, we should use @Bindable.
  • static let properties are always created lazily. This means they are only initialized when first accessed.
  • SwiftUI supports up to three views side by side in a single NavigationSplitView.
  • If we have two views in a navigation split view, NavigationLink presents destinations in the secondary view.

challenge

  1. Add a photo credit over the ResortView image. The data is already loaded from the JSON for this purpose, so you just need to make it look good in the UI.
  2. Fill in the loading and saving methods for Favorites.
  3. For a real challenge, let the user sort the resorts in ContentView either using the default order, alphabetical order, or country order.

solution

1. 사진의 출처 보여주기

사진 위에 캡션 형태로 image credit을 나타냈다.

                    .overlay(
                        VStack {
                            Spacer()
                            HStack {
                                Spacer()
                                Text(resort.imageCredit)
                                    .font(.caption)
                                    .padding()
                                    .foregroundColor(.white)
                                    .background(.black.opacity(0.5))
                                    .clipShape(.capsule)
                            }
                        }
                            .padding()
                    )

커밋 링크

2. 좋아요 목록 저장하고 불러오기 설정하기

Favorites의 resorts에 담은 데이터를 JSON 코딩을 통해 저장했다.

    init() {
        do {
            let data = try Data(contentsOf: savePath)
            let decoded = try JSONDecoder().decode(Set<String>.self, from: data)
            resorts = decoded
        } catch {
            resorts = []
        }
    }
    func save() {
        do {
            let data = try JSONEncoder().encode(resorts)
            try data.write(to: savePath, options: [.atomic, .completeFileProtection])
        } catch {
            print("Unable to save data.")
        }
    }

커밋 링크

3. resorts 정렬하기

📍 현재 아래 커밋을 시도해보고 안돼서 수정 중이다.

  • ListView를 만들어서 init에서 resorts의 순서를 설정해보기
    커밋 링크

  • filteredResorts를 @State로 설정해서 변경해보기(실패)
    커밋 링크

profile
계속 해보자

0개의 댓글

관련 채용 정보