【问题标题】:How to resolve Escaping closure captures 'inout' parameter 'data'如何解决转义闭包捕获'inout'参数'data'
【发布时间】:2021-03-27 19:13:38
【问题描述】:

我在一个单独的 swift 文件中有以下函数,我用它来对数据进行 Firebase 调用:

func fetchPosts(data: inout [Post]) {
    
    postRef.observe(DataEventType.value) { (snapshot) in
        
        data.removeAll()     // ***Error thrown here***
        
        if snapshot.value is NSNull {
            
            
        } else {
            
            var counter: UInt = 0
            
            for item in snapshot.children {
                
                let userID = (item as! DataSnapshot).key
                
                self.postRef.child(userID).observe(DataEventType.value) { (snap) in
                    
                    counter = counter + 1
                    
                    for child in snap.children {
                        
                        if let snapshot = child as? DataSnapshot,
                            
                            let post = Post(snapshot: snapshot) {

                                data.append(post)   // ***Error thrown here***
                                
                            }

                        if (counter == snapshot.childrenCount) {
                            
                            data = data.sorted(by: { $0.id > $1.id })   // ***Error thrown here***
                            
                        }
                        
                    }
                    
                }
                
            }
            
        }
        
    }
    
}

在我看来,我有以下几点:

@State var posts: [Post] = [] // which is the place holder for the posts data

然后我有以下电话

func fetchPosts() {
    
    postStore.fetchPosts(data: &posts)
    
}

调用上面的函数并通过引用传递[Post]数组

我的问题是我在上述函数中收到以下错误Escaping closure captures 'inout' parameter 'data',我无法弄清楚我需要做什么来解决它!?

有没有人遇到过这个错误,你会怎么解决它!?

【问题讨论】:

    标签: ios firebase firebase-realtime-database swiftui


    【解决方案1】:

    这不适用于@State,而是您必须使用完成回调模式,例如

    func fetchPosts() {
        self.posts = [] // reset
        postStore.fetchPosts() { newPosts in
           DispatchQueue.main.async {
             self.posts = newPosts
           }
        }
    }
    

    和获取功能

    func fetchPosts(completion: @escaping ([Post]) -> () ) {
    
    //  ...
            var data: [Post] = []   // data is a local variable
            
            // ... 
    
            if (counter == snapshot.childrenCount) {
                
                completion(data.sorted(by: { $0.id > $1.id }))
                
            }
    
    //  ...
    }
    

    【讨论】:

    • 您的解决方案抛出 3 个错误 1. postStore.fetchPosts() { newPosts in 抛出 Contextual closure type '() -> ([Post])' expects 0 arguments, but 1 was used in closure body 下一个是 2.DispatchQueue.main.async { 抛出 Cannot convert value of type '()' to closure result type '[Post]' 和最终 3. completion(self.posts.sorted(by: { $0.id > $1.id })) 抛出 Argument passed to call that takes no arguments
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2017-12-10
    • 1970-01-01
    相关资源
    最近更新 更多