Post

Replies

Boosts

Views

Activity

How can I display two Core Data entities in one view?
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.***)、\(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.***, 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() } }
0
1
726
Jun ’23
How can I display a text typed in Textfield on first view (page) to second view (page)?
Hi forum members, I am a beginner Swift programmer who is struggling to create my first prototype of SNS-type app. I am building an account creation page currently and I'm stacked at the stage to implement confirmation page for users to check if the information they input on the previous page is correct. I have now wrote the code below but it seems the code failed to display the user input on confirmation page. It seems the code stacked at the stage to pass information to Confirmation page. import SwiftUI struct AccountRegistrationView: View { @State private var userName = "" @State private var email = "" @State private var pass = "" var body: some View { NavigationStack { VStack() { Text("Welcome to XXXXXXXXX!") .font(.largeTitle) .fontWeight(.bold) .foregroundColor(.mint) .padding(10.0) Text("Enter your user name, email address and password.") .font(.body) .multilineTextAlignment(.center) .frame(width: 320.0, height: 50.0) } VStack { TextField("User name", text: $userName) .multilineTextAlignment(.center) .padding(.all, 20.0) .textFieldStyle(RoundedBorderTextFieldStyle()) TextField("Email address", text: $email) .multilineTextAlignment(.center) .padding(.all, 20.0) .textFieldStyle(RoundedBorderTextFieldStyle()) TextField("Password", text: $pass) .multilineTextAlignment(.center) .padding(.all, 20.0) .textFieldStyle(RoundedBorderTextFieldStyle()) } NavigationLink { ConfirmationView() } label: { Label("Next", systemImage: "arrowshape.forward.fill") .font(.body) } } } struct ConfirmationView: View { let userInfo = AccountRegistrationView() var body: some View { Text("User name: \(userInfo.userName)") .multilineTextAlignment(.center) .padding(.all, 20.0) Text("Email address: \(userInfo.email)") .multilineTextAlignment(.center) .padding(.all, 20.0) Text("Password: \(userInfo.pass)") .multilineTextAlignment(.center) .padding(.all, 20.0) } } struct AccountRegistrationView_Previews: PreviewProvider { static var previews: some View { AccountRegistrationView() } } } It would be much appreciated if you can point out which part of the code is causing the error and how to fix it. Thank you so much for your support in advance!
3
0
403
Jun ’23