iOS Swift & Parse - getDataInBackground 不工作?

标签 ios swift parse-platform

目前正在学习 Swift 和 iOS。我尝试使用 Parse 访问保存的图片。但是,我无法使用 getDataInBackground(block:) 访问它。

这是我的代码:

//
//  ViewController.swift
//  Instragram
//
//  Created by Macbook Pro on 22.07.17.
//  Copyright © 2017 Macbook Pro. All rights reserved.
//

import UIKit
import Parse

class ViewController: UIViewController {
    @IBOutlet weak var picture: UIImageView!
    @IBOutlet weak var senderLbl: UILabel!
    @IBOutlet weak var recieverLbl: UILabel!
    @IBOutlet weak var messageLbl: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Creating tables and data in database
        let imageData = UIImageJPEGRepresentation(picture.image!, 0.5)

        let file = PFFile(name: "picture.jpg", data: imageData!)

        let table = PFObject(className: "messages")
        table["sender"] = "Akhmed"
        table["reciver"] = "Bob"
        table["picture"] = file
        table["message"] = "Hello!"
        table.saveInBackground {(success, error) -> Void in
            if(success){
                print("Saved successful")
            } else {
                print(error!)
            }
        }

        //Recieving Data from the Server
        let information = PFQuery(className: "messages")
        information.findObjectsInBackground{(objects: [PFObject]?, error) -> Void in
            if error == nil {
                for object in objects!{
                    self.senderLbl.text = object["sender"] as? String
                    self.recieverLbl.text = object["reciver"] as? String
                    self.messageLbl.text = object["message"] as? String

                    object["picture"].getDataInBackground(...)
                }
            } else {
                print(error!)
            }
        }
    }
}

在访问名称、接收者和消息字符串后,我尝试访问保存在服务器上的图像:

object["picture"].getDataInBackground(block:) 

但是,在我输入 object["picture"] 后,Swift 甚至不再自动更正。我也收到一个错误:

'Value of type "Any" has no Member 'getDataInBackground(block:)'

有什么问题吗?在我看来,Swift 找不到字符串 picture 即使图像保存在服务器上的字符串“picture”下。

最佳答案

您需要先将其转换为 PFFfile 对象,然后使用 getDataInBackground 函数检索实际图像数据,如下所示:

let imageFile = object["picture"] as? PFFile
imageFile?.getDataInBackground (block: { (data, error) -> Void in
      if error == nil {
                     if let imageData = data {
                         self.myImage = UIImage(data:imageData)
                     }
      }
})

关于iOS Swift & Parse - getDataInBackground 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45269046/

相关文章:

ios - 为什么不能将 PFObject 添加到 PFRelation?

ios - 如何快速循环保存在后台

ios - 在 IOS 上,parentViewController 返回 null

ios - NSDateFormatter 通过简单的请求返回 nil 日期

ios - 如何在滚动 Collection View 内的 TableView 时隐藏导航栏

ios - Swift Parse - 使用 PFQuery 或 QueryWithSubqueries 函数

iOS 如何只展开一个单元格

ios - iOS-合并-将发布者类型更改为子类型

objective-c - 带有闭包的 Swift init 在 Objective C 中不可见

node.js - 在 node.js 中使用带有查询的 Parse.com REST API - 如何使用来自 CURL 的数据编码