UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: I can not use instance of a struct (X) to create another struct (Y) that has parameter type X.

Forums > SwiftUI

Hello,

For practice I am trying to create simple code similar something to instagram. However I am getting an error while trying to create post instance.

If I change the "let post =..." line as below, it works. let post = Post(user: User(username: "john_doe", profileImageName: "profile_image"), imageName: "image_name", caption: "This is a great image!", timestamp: "1h", likes: 100, comments: [])

Why I can not use user instance while creating post instance?

Thanks.

import SwiftUI

struct User {
    var username: String
    var profileImageName: String
}

struct Post: Identifiable {
    var id = UUID()
    var user: User
    var imageName: String
    var caption: String
    var timestamp: String
    var likes: Int
    var comments: [Comment]

}

struct Comment {
    var user: User
    var text: String
}

struct MyFeedView: View {

    var posts: [Post]
    var body: some View {
        List {
            ForEach(posts) { post in
                VStack(alignment: .leading) {
                    HStack {
                        Image(post.user.profileImageName)
                            .resizable()
                            .frame(width: 50, height: 50)
                            .clipShape(Circle())
                        VStack(alignment: .leading) {
                            Text(post.user.username)
                                .font(.headline)
                            Text(post.timestamp)
                                .font(.subheadline)
                                .foregroundColor(.gray)
                        }
                        Spacer()
                        Button(action: {
                            // handle button tap
                        }) {
                            Image(systemName: "ellipsis.circle")
                                .font(.system(size: 20))
                        }
                    }
                    Image(post.imageName)
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                    HStack {
                        Button(action: {
                            // handle like button tap
                        }) {
                            Image(systemName: "heart")
                                .font(.system(size: 20))
                        }
                        Button(action: {
                            // handle comment button tap
                        }) {
                            Image(systemName: "bubble.left")
                                .font(.system(size: 20))
                        }
                        Spacer()
                        Button(action: {
                            // handle share button tap
                        }) {
                            Image(systemName: "arrowshape.turn.up.left")
                                .font(.system(size: 20))
                        }
                    }
                    Text(post.caption)
                        .font(.body)
                }
            }
        }
    }
}

struct ContentView: View {
    let user = User(username: "john_doe", profileImageName: "profile_image")
    let post = Post(user: user, imageName: "image_name", caption: "This is a great image!", timestamp: "1h", likes: 100, comments: [])

    var body: some View {

        MyFeedView(posts: [post])
    }

}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

2      

can you edit your post to capture the code in backticks to make it easierto read

3      

You need to set the variable post in the initializer. You can't use a variable name to declare another variable in this place you're trying. You don't need to set the User in the declaration of the initializer. I just did it out of convienece.

struct ContentView: View {
    let user: User
    let post: Post

    init(user: User = User(username: "john_doe", profileImageName: "profile_image")) {
        self.user = user
        self.post = Post(user: user, imageName: "image_name", caption: "This is a great image!", timestamp: "1h", likes: 100, comments: [])
    }

    var body: some View {

        MyFeedView(posts: [post])
    }

}

2      

Why I can not use user instance while creating post instance?

In Swift, instances of structs or classes have to be fully initialized before you can use them. Since "use them" includes accessing any of their properties, that means you cannot initialize a property using the value of another property, as you are trying to do here when you use the value of user to create post:

let user = User(username: "john_doe", profileImageName: "profile_image")
let post = Post(user: user, imageName: "image_name", caption: "This is a great image!", timestamp: "1h", likes: 100, comments: [])

So, instead, you have to do what @Hatsushira shows you.

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.