Hello,
I’m working on Socialcademy 4, but when I get to the Before we continue, we’ll need to fix another compiler error. I’ll come, I’ll move on to the solution. The error does not go away for me. I copied the code, but this doesn’t help. Does anyone have another solution?
code Loadable
import Foundation
enum Loadable {
case loading
case error(Error)
case loaded(Value)
var value: Value? {
get {
if case let .loaded(value) = self {
return value
}
return nil
}
set {
guard let newValue = newValue else { return }
self = .loaded(newValue)
}
}
}
extension Loadable where Value: RangeReplaceableCollection {
static var empty: Loadable { .loaded(Value())}
}
extension Loadable: Equatable where Value: Equatable {
static func == (lhs: Loadable, rhs: Loadable) → Bool {
switch (lhs, rhs) {
case (.loading, .loading):
return true
case let (.error(error1), .error(error2)):
return error1.localizedDescription == error2.localizedDescription
case let (.loaded(value1), .loaded(value2)):
return value1 == value2
default:
return false
}
}
}
code PostList
import SwiftUI
struct PostList: View {
@StateObject var viewmodel = PostsViewModel()
@State private var searchText = ""
@State private var showNewPostForm = false
var body: some View {
NavigationView {
Group {
switch viewmodel.posts {
case .loading:
ProgressView()
case .error(error):
VStack (alignment: .center, spacing: 10) {
Text("Cannot load Posts")
.font(.title2)
.fontWeight(.semibold)
.foregroundColor(.primary)
}
case .empty:
Text("No Posts")
case let .loaded(posts):
List(posts) { post in
if searchText.isEmpty || post.contains(searchText) {
PostRow(post: post)
}
}
.searchable(text: $searchText)
}
.searchable(text: $searchText)
.navigationTitle("Posts")
.toolbar {
Button {
showNewPostForm = true
} label: {
Label("New Post", systemImage: "square.and.pencil")
}
}
.sheet(isPresented: $showNewPostForm) {
NewPostForm(createAction: viewmodel.makeCreateAction())
}
}
.onAppear {
viewmodel.fetchPosts()
}
}
}
}
#Preview {
PostList()
}