Return 功能完成值

Return Value in Completion of Function

我已经为 MySQL 数据库上的 运行 查询构建了自己的 swift 框架。我目前正在开发一个聊天应用程序,我需要在其中为当前正在聊天的每个用户显示头像图像。

我正在从我的数据库中检索用户的个人资料图片 URL,然后我需要 return 获取个人资料图片 URL 后 UIImage。我收到错误 Unexpected non-void return value in void function。有没有办法让这更容易或绕过这个错误?

这是我的完整代码:

    override func collectionView(collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageAvatarImageDataSource! {

    let currentMessage = self.messages[indexPath.row]

    if (currentMessage.senderId == self.senderId) {
        // Current User sent message

        var imageURL: NSURL!

        ConnectionManager.sharedInstance.complexQuery("SELECT profile_picture FROM users WHERE username='\(self.senderId)' ", completion: { (result) in
            if let results = result as? [[String: AnyObject]] {
                for result in results {
                    if let profilePictureURL = result["profile_picture"] {
                        print(profilePictureURL)

                        imageURL = profilePictureURL as! NSURL
                        let imageData = NSData(contentsOfURL: imageURL)
                        let image = UIImage(data: imageData!)

                        // ERROR HERE (line below)
                        return JSQMessagesAvatarImageFactory.avatarImageWithImage(image, diameter: UInt(kJSQMessagesCollectionViewAvatarSizeDefault))
                    }
                }
            }
        })

    } else {

        var imageURL: NSURL!

        ConnectionManager.sharedInstance.complexQuery("SELECT profile_picture FROM users WHERE username='\(currentMessage.senderId)' ", completion: { (result) in
            if let results = result as? [[String: AnyObject]] {
                for result in results {
                    if let profilePictureURL = result["profile_picture"] {
                        print(profilePictureURL)

                        imageURL = profilePictureURL as! NSURL
                        let imageData = NSData(contentsOfURL: imageURL)
                        let image = UIImage(data: imageData!)

                        // ERROR HERE (line below)
                        return JSQMessagesAvatarImageFactory.avatarImageWithImage(image, diameter: UInt(kJSQMessagesCollectionViewAvatarSizeDefault))
                    }
                }
            }
        })

    }
}

好像ConnectionManager.sharedInstance.complexQuery是一个异步函数,所以主线程调用后会继续执行,这个上下文就没有什么可以接收返回的了 所以,在回调函数中用图像做任何你想做的事 "completion" 例如,您可能会获取 UIImageView 的引用并在获取它时设置它的图像,但是您需要使用另一个数据源委托方法

collectionView(_:cellForItemAtIndexPath:)

像这样:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(YOUR_ID,
                               forIndexPath: indexPath) as! YourOwnCell
        ConnectionManager.sharedInstance.complexQuery("SELECT profile_picture FROM users WHERE username='\(currentMessage.senderId)' ", completion: { (result) in
            if let results = result as? [[String: AnyObject]] {
                for result in results {
                    if let profilePictureURL = result["profile_picture"] {
                        print(profilePictureURL)

                        imageURL = profilePictureURL as! NSURL
                        let imageData = NSData(contentsOfURL: imageURL)
                        let image = UIImage(data: imageData!)

                        // Assuming avatarImageWithImage is returning UIImageView
                        cell.imageView = JSQMessagesAvatarImageFactory.avatarImageWithImage(image, diameter: UInt(kJSQMessagesCollectionViewAvatarSizeDefault))
                    }
                }
            }
        })

        return cell;
    }

您尝试的不是异步函数的工作方式。这不是他们可以的工作方式。异步调用的全部意义在于您的应用可以继续 运行,而异步调用在后台继续 运行。你的 complexQuery 函数 returns 立即让你可以继续。

此函数返回您想要的值,您的完成代码必须将值存放在正确的位置。