Hi forum members,
I am a beginner Swift coder who is struggling to create my first prototype of SNS-type app. I am building main view of the app which users can see other user posts by scrolling down (it would be easier to understand if you imagine a scrolling screen like the home screen of Instagram). I am attempting to use Core Data to register data and display posts on the view. I have registered two entities to display on the view which is "Post" entity and "User" entity in my Core Data. I wrote a code as below but then I get an error saying Static method 'buildExpression' requires that 'some View' conform to 'AccessibilityRotorContent' at ForEach in my code. I searched and tried to solve this error by replacing the var body: some View with Self.Body but then I get different error at struct SampleUserData: View. I am totally stacked here and that is why I need your huge support.
The "User" entity actually has a One to Many relationship with "Post" entity since user can have multiple posts. I don't think I am writing my code correctly for displaying the "User" and "Post" entities by using the relationship so I would be much appreciated if you can also teach me how to fetch two entities with relationship as well.
I totally appreciate for your support in advance and forgive me if how I ask support is wrong since I'm beginner to Forum as well. Please let me know if you need more information to correct the code.
Below if my full code.
import CoreData
import SwiftUI
struct SampleUserData: View {
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(
entity: Post.entity(),
sortDescriptors: [NSSortDescriptor(keyPath: \Post.timestamp, ascending: false)],
predicate: nil
) private var posts: FetchedResults<Post>
@FetchRequest(
entity: User.entity(),
sortDescriptors: [NSSortDescriptor(keyPath: \User.username, ascending: false)],
predicate: nil
) private var users: FetchedResults<User>
var body: some View {
NavigationStack {
ScrollView {
VStack {
//Top banner start
HStack {
Image("c85b9ce6-859a-4683-8ae5-f83d16ef04f0")
.resizable(resizingMode: .stretch)
.aspectRatio(contentMode: .fill)
.frame(width: 50, height: 50)
.clipShape(Circle())
.padding(.leading, 30)
Spacer()
Image("Logo")
.resizable(resizingMode: .stretch)
.aspectRatio(contentMode: .fit)
.frame(width: 50, height: 50)
.foregroundColor(.mint)
Spacer()
Image(systemName: "square.and.arrow.up.fill")
.frame(width: 50, height: 50)
.font(.system(size: 40))
.foregroundColor(.mint)
.padding(.trailing, 30)
} // Top banner end
.padding(.bottom, 10.0)
Divider()
.frame(height: 1.5)
.background(Color.mint)
// Post Content start
HStack {
ForEach(users, id: \.self) { user in
Image(systemName: "square.and.arrow.up.fill")
.resizable(resizingMode: .stretch)
.aspectRatio(contentMode: .fill)
.frame(width: 50, height: 50)
.clipShape(Circle()) //Here I get error "Static method 'buildExpression' requires that 'some View' conform to 'AccessibilityRotorContent', 1. Where 'Content' = 'some View' (SwiftUI.AccessibilityRotorContentBuilder)"
VStack(alignment: .leading) {
Text("\(user.username)")
.fontWeight(.heavy)
Text("\(user.age)代\(user.sex)、\(user.location)")
}
Spacer()
.padding(.leading, 30)
VStack {
TabView {
Image(systemName: "folder.fill")
.resizable()
.aspectRatio(contentMode: .fit)
.padding()
}
.frame(width: 350.0, height: 350.0)
.tabViewStyle(PageTabViewStyle())
HStack {
ForEach(posts, id: \.self) { post in
NavigationLink {
PostDetailView(
userPhoto: "user.userPhotos", userName: user.username, userAge: user.age,
userSex: user.sex, userLocation: user.location,
postImage: "user.postImages",
postTitle: post.title, postLike: post.likes,
postShare: post.shares,
postContent: post.content)
} label: {
Text("\(post.title)")
.font(.title2)
.fontWeight(.heavy)
.foregroundColor(Color.black)
.padding()
.frame(width: 250, height: 100)
}
Spacer()
VStack {
Image(systemName: "heart.fill")
.font(.system(size: 20))
.foregroundColor(.mint)
Text("\(post.likes)")
}
.frame(width: 50, height: 50)
Spacer()
VStack {
Image(systemName: "arrowshape.turn.up.right.fill")
.font(.system(size: 20))
.foregroundColor(.mint)
Text("\(post.shares)")
}
.frame(width: 50, height: 50)
VStack {
Text("\(post.content)")
.lineLimit(2)
} // PostContentSection
.padding(.vertical, 5)
}
}
}
.padding(.leading, 20)
.padding(.trailing, 20)
Divider()
.frame(height: 1.5)
.background(Color.mint)
}
} // Post content end
}
Spacer()
}
}
}
}
struct SampleUserData_Previews: PreviewProvider {
static var previews: some View {
SampleUserData()
}
}