How to make sure data in variable is loaded before using it

前端 未结 2 1090
一个人的身影
一个人的身影 2021-01-28 15:57

So I have an .onRecieve method

.onReceive(model2.$postsById) { postById in
       print(\"MODEL2 \\(self.model2.postsById)\")
    }
相关标签:
2条回答
  • 2021-01-28 16:35

    try this:

    struct Album: View {
        var post:Post
        var post2:[PostById]
    
        var body: some View {
                 VStack {
                    Text("Title: ").bold()
                        + Text("\(post!.title)")
                    ImageView(withURL: "http://localhost:8000/\(post!.path.replacingOccurrences(of: " ", with: "%20"))")
                        Text("Description: ").bold()
                            + Text("\(post!.description)")
    
    if post2.count > 0 {
                List(self.post2.filter { i in i.album_id == post.id }) { post       in
                    Text("\(post.name ?? "title")")
                    print(post2)
                 }
    }
    
    0 讨论(0)
  • 2021-01-28 16:51

    I think you want to declare post2 as

     @Binding var post2:[PostById]
    

    And then pass in

     Album(post: post, post2: self.model2._postsById)
    

    Which is what $ does, but you can't use that here.

    0 讨论(0)
提交回复
热议问题