嵌套结构模型不会导致视图重新呈现SwiftUI

嵌套结构模型不会导致视图重新呈现SwiftUI,swiftui,Swiftui,我有一个视图可以通过和ObservieObject监听模型: class Feed : ObservableObject { // Posts to be displayed @Published var posts = [Posts]() ... ... } Posts模型看起来像: struct Posts: Hashable, Identifiable { let bar: Bars let time: String va

我有一个视图可以通过和ObservieObject监听模型:

class Feed : ObservableObject {
    
    // Posts to be displayed
    @Published var posts = [Posts]()
    ...
    ...
}
Posts模型看起来像:

struct Posts: Hashable, Identifiable {
    let bar: Bars
    let time: String
    var description: String
    let id: String
    let createdAt : String
    let tags : [Friends]
    let groups : [String]
    var intializer : Friends    // Creator of the post
}
struct Friends : Hashable {
    
    static func == (lhs: Friends, rhs: Friends) -> Bool {
        return lhs.id == rhs.id
    }
    
    let name: String
    let username: String
    let id : String
    var thumbnail : UIImage?
    var profileImgURL : String?
    
    func hash(into hasher: inout Hasher) {
        hasher.combine(id)
    }
}
它包含多个其他结构模型,如Friends和Bars。但是,当我在这些其他模型中更改一个值时,它不会触发@Published to fire,因此不会重新绘制视图。例如,Friends模型如下所示:

struct Posts: Hashable, Identifiable {
    let bar: Bars
    let time: String
    var description: String
    let id: String
    let createdAt : String
    let tags : [Friends]
    let groups : [String]
    var intializer : Friends    // Creator of the post
}
struct Friends : Hashable {
    
    static func == (lhs: Friends, rhs: Friends) -> Bool {
        return lhs.id == rhs.id
    }
    
    let name: String
    let username: String
    let id : String
    var thumbnail : UIImage?
    var profileImgURL : String?
    
    func hash(into hasher: inout Hasher) {
        hasher.combine(id)
    }
}
但当我更改缩略图时,视图不会重新绘制。但是当我直接改变Posts模型之外的内容时,比如
description
属性,视图就会被重新绘制。当基础模型值发生更改时,如何重新绘制视图

我更改缩略图,如图所示:

        // Grab the thumbnail of user (if exists)
        if post.intializer.profileImgURL != nil {
            AF.request(post.intializer.profileImgURL!, method: .get, encoding: URLEncoding.default)
                .validate()
                .responseData { (response) in
                    if let data = response.value {
                        // Find the index of where this post is in the array and set the profile img
                        if let indexOfPost = self.posts.firstIndex(of: post) {
                            self.posts[indexOfPost].intializer.thumbnail = UIImage(data: data)
                        }
                    }
            }
        }
但如果我改变描述做同样的事情:

        // Grab the thumbnail of user (if exists)
        if post.intializer.profileImgURL != nil {
            AF.request(post.intializer.profileImgURL!, method: .get, encoding: URLEncoding.default)
                .validate()
                .responseData { (response) in
                    if let data = response.value {
                        // Find the index of where this post is in the array and set the profile img
                        if let indexOfPost = self.posts.firstIndex(of: post) {
                            self.posts[indexOfPost].description = "Loaded!!!!"
                        }
                    }
            }
        }
当我这样做时,视图会更新和更改。我可以看到缩略图也被正确加载,因为我可以打印出发送的数据,有时缩略图被正确地重新绘制

编辑

正如我建议的那样,我尝试向结构添加一个变异func:

struct Posts: Hashable, Identifiable {
    let bar: Bars
    let time: String
    var description: String
    let id: String
    let createdAt : String
    let tags : [Friends]
    let groups : [String]
    var intializer : Friends    // Creator of the post
    
    mutating func addInitThumbnail(img : UIImage) {
        self.intializer.thumbnail = img
    }
}
然后使用它:

    func grabInitThumbnail(post : Posts) {
        // Grab the thumbnail of user (if exists)
        if post.intializer.profileImgURL != nil {
            AF.request(post.intializer.profileImgURL!, method: .get, encoding: URLEncoding.default)
                .validate()
                .responseData { (response) in
                    if let data = response.value {
                        // Find the index of where this post is in the array and set the profile img
                        if let indexOfPost = self.posts.firstIndex(of: post) {
                            if let thumbnailImg = UIImage(data: data) {
                                self.posts[indexOfPost].addInitThumbnail(img: thumbnailImg)
                            }
                        }
                    }
            }
        }
    }
但它也不起作用

然而,当我这样做时:

    func grabInitThumbnail(post : Posts) {
        // Grab the thumbnail of user (if exists)
        if post.intializer.profileImgURL != nil {
            AF.request(post.intializer.profileImgURL!, method: .get, encoding: URLEncoding.default)
                .validate()
                .responseData { (response) in
                    if let data = response.value {
                        // Find the index of where this post is in the array and set the profile img
                        if let indexOfPost = self.posts.firstIndex(of: post) {
                            self.posts[indexOfPost].intializer.thumbnail = UIImage(data: data)
                            self.posts[indexOfPost].description = "Loaded!!!!"
                        }
                    }
            }
        }
    }

图像已加载并正确设置。。。?所以我认为这可能与UIImages直接有关?

我尝试使用变异函数,并直接更新值,这两种情况都有效

更新代码(在新结构中添加UIImage)


当你说“我更改缩略图”时,你是在设置新的缩略图,还是在更改UIImage的属性?将缩略图从零设置为UIImage。你能告诉我你是如何更改缩略图值的吗@JakeMaschoff@Zeona我在如何更改缩略图值的问题中添加了,如果需要,请告诉我helps@JakeMaschoff请尝试使用
mutating
检查更新是否有效。例如,添加一个变异函数来更改结构中的缩略图,并调用该函数。mutating func updateThumbnail(缩略图:UIImage){self.thumbnail=thumbnail)所以我尝试了这个方法,效果很好,但当我把嵌套字段设置为UIImage时,它不会更新…所以我认为它与UIImage有直接关系?@JakeMaschoff我尝试过图像,效果也很好。我不知道你的具体问题是什么。我会添加更新的代码。请检查。这很好。广告对结构的
update
方法进行修改
mutation
修复了我的重新渲染问题。