iOS Swift&解析 - getDataInBackground不工作?

时间:2017-07-23 19:21:15

标签: 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甚至不再自动更正。我也得到了一个错误:

  

'类型的值“任何”没有成员'getDataInBackground(block:)'

任何想法有什么不对?在我看来,即使图像在字符串“picture”下保存在服务器上,Swift也找不到字符串picture

1 个答案:

答案 0 :(得分:1)

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

getDataInBackground