将数据从Parse服务器加载到数组中的模型文件中。

时间:2017-07-02 01:08:35

标签: swift parse-platform parse-server

我想将解析服务器中的数据加载到名为posts的数组中的模型文件中,但我不知道如何。

以下是代码:

struct Post

{     var createdBy:用户     var timeAgo:String?     var caption:String?     var image:UIImage?     var numberOfLikes:Int?     var numberOfComments:Int?     var numberOfShares:Int?

static func fetchPosts() -> [Post]
{
    var posts = [Post]()

    var usernameArray = [String]()
    var avaArray = [PFFile]()
    var dateArray = [Date?]()
    var picArray = [PFFile]()
    var titleArray = [String]()
    var uuidArray = [String]()


    let query = PFQuery(className: "posts")
    query.findObjectsInBackground { (objects: [PFObject]?, error: Error?) in
        if error == nil {
            usernameArray.removeAll(keepingCapacity: false)
            avaArray.removeAll(keepingCapacity: false)
            dateArray.removeAll(keepingCapacity: false)
            picArray.removeAll(keepingCapacity: false)
            titleArray.removeAll(keepingCapacity: false)
            uuidArray.removeAll(keepingCapacity: false)

            // find related objects
            for object in objects! {



                usernameArray.append(object.object(forKey: "username") as! String)
                avaArray.append(object.object(forKey: "ava") as! PFFile)
                dateArray.append(object.createdAt)
                picArray.append(object.object(forKey: "pic") as! PFFile)
                titleArray.append(object.object(forKey: "title") as! String)
                uuidArray.append(object.object(forKey: "uuid") as! String)
            }

        }
    }

     return posts
}

}

struct User {     var username:String?     var profileImage:UIImage? }

1 个答案:

答案 0 :(得分:0)

试试这个

static func fetchPosts() -> [Post]
{
    var posts = [Post]()

    var usernameArray = [String]()
    var avaArray = [PFFile]()
    var dateArray = [Date?]()
    var picArray = [PFFile]()
    var titleArray = [String]()
    var uuidArray = [String]()


    let query = PFQuery(className: "posts")
    query.findObjectsInBackground { (objects: [PFObject]?, error: Error?) in
        if error == nil {                
            // find related objects
            if let returnedObjects = objects {
                for object in returnedObjects {
                    usernameArray.append(object["username"] as! String)
                    avaArray.append(object["ava"] as! PFFile)
                    dateArray.append(object.createdAt)
                    picArray.append(object["pic" as! PFFile)
                    titleArray.append(object["title"] as! String)
                    uuidArray.append(object["uuid"] as! String)
                }
            }
        }
    }

    return posts
}

要将数据附加到您需要的posts - 将PFFile转换为UIImage - 使用UIImage创建User对象 - 将用户对象添加到posts

if let avaPicture = object["ava"]! as! PFFile {
    avaPicture.getDataInBackground({ (imageData: Data?, error: Error?) -> Void in
        if (error == nil) {
            let image = UIImage(data: imageData!)
            if image != nil {
                // Create your User object with UIImage here
                let user = User()...

                // Add the new user and other data to your `posts`array
                ...
            }
        }
    })
}