WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

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      

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.