【问题标题】:Thread 1 error when trying to update variable SwiftUI Instagram Tutorial尝试更新变量 SwiftUI Instagram 教程时出现线程 1 错误
【发布时间】:2020-09-29 07:33:31
【问题描述】:

我是编码新手,目前正在学习使用 SwiftUI 的 instagram 教程,为了提高我的技能,我一直在调整我的布局以尝试新概念。

我目前遇到“更新点赞数”的问题。本教程显示了 likeCount 变量的典型 instagram 布局,它是点赞按钮下“页脚”(图像下方)的一部分,但我已经采用了方法(类似于 Vero),在标题和点赞按钮上有了点赞按钮数到页脚。

查看当前代码:

        import Foundation
    import SwiftUI
    import FirebaseAuth

class HeaderCellViewModel: ObservableObject {
    @Published var post: Post!
    @Published var isLoading = false
    @Published var isLiked = false
    
    func checkPostIsLiked() {
        isLiked = (post.likes["\(Auth.auth().currentUser!.uid)"] == true) ? true : false
    }

    func like() {
        post.likeCount += 1
        isLiked = true
        
        Ref.FIRESTORE_MY_POSTS_DOCUMENT_USERID(userId: post.ownerId).collection("userPosts").document(post.postId).updateData(["likes.\(Auth.auth().currentUser!.uid)" : true,
                                                                                    "likeCount": post.likeCount])
        Ref.FIRESTORE_COLLECTION_ALL_POSTS.document(post.postId).updateData(["likes.\(Auth.auth().currentUser!.uid)" : true,
                                                                             "likeCount": post.likeCount])
        Ref.FIRESTORE_TIMELINE_DOCUMENT_USERID(userId: post.ownerId).collection("timelinePosts").document(post.postId).updateData(["likes.\(Auth.auth().currentUser!.uid)" : true,
                                                                                        "likeCount": post.likeCount])
    }
    
    func unlike() {
        post.likeCount -= 1
        isLiked = false
        
        Ref.FIRESTORE_MY_POSTS_DOCUMENT_USERID(userId: post.ownerId).collection("userPosts").document(post.postId).updateData(["likes.\(Auth.auth().currentUser!.uid)" : false,
                                                                                    "likeCount": post.likeCount])
        Ref.FIRESTORE_COLLECTION_ALL_POSTS.document(post.postId).updateData(["likes.\(Auth.auth().currentUser!.uid)" : false,
                                                                             "likeCount": post.likeCount])
        Ref.FIRESTORE_TIMELINE_DOCUMENT_USERID(userId: post.ownerId).collection("timelinePosts").document(post.postId).updateData(["likes.\(Auth.auth().currentUser!.uid)" : false,
                                                                                        "likeCount": post.likeCount])
    }

}


    import SwiftUI

    import SwiftUI
import URLImage

struct HeaderCell: View {
    
    @ObservedObject var headerCellViewModel = HeaderCellViewModel()
    
    init(post: Post) {
        self.headerCellViewModel.post = post
        self.headerCellViewModel.checkPostIsLiked()
    }

 if headerCellViewModel.post.likeCount > 0 {
                Text("\(headerCellViewModel.post.likeCount)")
            }
            Image(systemName: (self.headerCellViewModel.isLiked) ? "hand.thumbsup.fill" : "hand.thumbsup").resizable().scaledToFill().frame(width: 30, height: 30).foregroundColor(.gray).onTapGesture {
                if self.headerCellViewModel.isLiked {
                    self.headerCellViewModel.unlike()
                } else {
                    self.headerCellViewModel.like()
                }
            }

struct FooterCell: View {
    
    @ObservedObject var footerCellViewModel = FooterCellViewModel()
    @ObservedObject var headerCellViewModel = HeaderCellViewModel()
    
    init(post: Post) {
        self.footerCellViewModel.post = post
        self.footerCellViewModel.checkPostIsLiked()
    }

    var body: some View {
        VStack(alignment: .leading, spacing: 8) {
            Text(self.footerCellViewModel.post.caption).font(.subheadline).padding(.leading)
            
            HStack {
                Image(systemName: "mappin.and.ellipse").foregroundColor(.gray).imageScale(.small)
                Text("Location").font(.caption).foregroundColor(.gray)
            }.padding(.leading)
            
            HStack {
                
                NavigationLink(destination: CommentView(post: self.footerCellViewModel.post)) {
                    Text("4K")
                    Image(systemName: "text.bubble").renderingMode(.original)
                }
                Spacer().frame(width: 15)
                NavigationLink(destination: Text("Likes")) {
                    if footerCellViewModel.post.likeCount > 0 {
                        Text("\(footerCellViewModel.post.likeCount)")
                    } else {
                      Text("0")
                    }
                    
                    Image(systemName: "hand.thumbsup").renderingMode(.original)
                }

我的问题是,当我从 footerCell 引用 headerCellViewModel 时,它会出现“线程 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)”并崩溃。我复制了 headerCellViewModel 并将其设置为页脚,但是当我现在按下“赞”按钮时,视图计数不会实时更新。他们只是不互相交谈,我不确定该怎么做。

【问题讨论】:

    标签: xcode variables swiftui instagram


    【解决方案1】:

    您还需要将post 分配给标题视图模型(因为它在声明中明确强制解包,即var post: Post!

    struct FooterCell: View {
        
        @ObservedObject var footerCellViewModel = FooterCellViewModel()
        @ObservedObject var headerCellViewModel = HeaderCellViewModel()
        
        init(post: Post) {
            self.footerCellViewModel.post = post
            self.headerCellViewModel.post = post    // << here !!
     
    

    【讨论】:

    • 感谢 Asperi 的回复!我添加了它,但它似乎仍然没有在页脚单元格上更新。你知道为什么我不能直接引用 headerCell 这样我就不必复制视图模型的原因吗?
    • SwiftUI 本质上是响应式的,您应该使用模型进行操作,如果模型发生更改,视图将自动更新。如果您希望在相同的更改上更新页眉和页脚,您必须使用相同的 HeaderCellViewModel 实例,现在您为每个视图创建自己的 - 而是创建一个并在两个视图中传递参数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-15
    • 2011-03-31
    • 1970-01-01
    相关资源
    最近更新 更多