|
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.
|
|
|
|
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.
|
|
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
So, instead, you have to do what |
SPONSORED AppSweep by Guardsquare helps developers automate the mobile app security testing process with fast, free scans. By using AppSweep’s actionable recommendations, developers can improve the security posture of their apps in accordance with security standards like OWASP.
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.