Swift:如何获得Parse的许多用户名和照片

时间:2015-11-09 10:12:38

标签: ios swift parse-platform swift2

我的消息应用中有很多用户,我的消息是"消息" class我需要从我们应用程序的Parse配置文件中获取发件人的姓名和个人资料图片,以便在tableView上的每条消息中显示它们。

我只想在tableView中显示类中用户的名称" messages"包含在列"发件人"它包含指向PFUsers的指针(我需要" first_name"," last_name"," profile_picture")

我的用户类

enter image description here

我的留言课

enter image description here

更新!

无法解决问题所在,如果我知道某事,必须更改其他内容。这里更新的代码:

findTimeLineDataQuery.includeKey("sender")


findTimeLineDataQuery.findObjectsInBackgroundWithBlock({
    (objects : [AnyObject]?, error : NSError?) in

    if let objects = objects where error == nil {

        var chats:(timelinechatsData: [String], chatsDate: [NSDate]) = ([], []) //date might be NSDate
        var message: (messageObjts: [String], messageSender: [String]) = ([], [])

        var datesToString: [String] {
            get {
                var stringDates:[String] = []
                let formatter = NSDateFormatter()
                formatter.dateFormat = "dd-MM-yyyy" //change the format as you wish
                for date in dates {
                    stringDates.append(formatter.stringFromDate(date))
                }
                return stringDates
            }
        }

        for singleObject in objects {

            if let user = singleObject["sender"] as? PFObject {
                let first_name = user["first_name"] as! String
                let last_name = user["last_name"] as! String

                //Cast it to how you saved it. I can't see it from the picture so I assumed you saved it as NSData
                let picture = user["picture"] as! NSData
                self.picturesProfilesArray.append(picture)
                //once you fetch the user data, you can save it in a dictionary or however you want and later call it from cellForRowAtIndex
            }



            if let stringData = singleObject["message"] as? String {

                chats.timelinechatsData.append(stringData)

                if let messageDate = singleObject.createdAt {
                    chats.chatsDate.append(messageDate!)
                }
            }

            //new
            if let messageObject = singleObject["messageObject"] as? String {
                message.messageObjts.append(messageObject)
            }

            //new senderNickname
            if let messageSender = singleObject["senderNickname"] as? String {
                message.messageSender.append(messageSender)
            }
        }

        //update self
        dispatch_async(dispatch_get_main_queue()) {

            self.timelineChatsDataArray += chats.timelinechatsData
            self.chatsDateArray += datesToString
            self.messageObjectArray += message.messageObjts
            self.messageSenderArray += message.messageSender

            self.tableView.reloadData()
        }
    }
})

更改

所以我的

var chatsDateArray : [NSDate] = []

let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "MM-dd-yyyy HH:mm"
        let useDate = dateFormatter.stringFromDate(self.chatsDateArray[indexPath.row])
        cell.dateMessageLabel.text = useDate

应该成为

    var chatsDateArray : [String] = []

let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "MM-dd-yyyy HH:mm"
        let useDate = String(self.chatsDateArray[indexPath.row])
        cell.dateMessageLabel.text = useDate

但这会导致:

Initialization of immutable value 'first_name' was never used; consider replacing with assignment to '_' or removing it

Initialization of immutable value 'last_name' was never used; consider replacing with assignment to '_' or removing it

这会通过将var chat(chatsDate)更改回字符串而消失 ,但这会导致

 Cannot convert value of type 'NSDate' to expected argument type 'String'

on

if let messageDate = singleObject.createdAt {
                        chats.chatsDate.append(messageDate!)
                    }
                }

1 个答案:

答案 0 :(得分:1)

您可以使用' includeKey'访问相关类的指针值

findTimeLineDataQuery.includeKey("sender")

findTimeLineDataQuery.findObjectsInBackgroundWithBlock({
    (objects : [AnyObject]?, error : NSError?) in

    if let objects = objects where error == nil {

        var chats:(timelinechatsData: [String], chatsDate: [NSDate]) = ([], []) //date might be NSDate
        var message: (messageObjts: [String], messageSender: [String]) = ([], [])

        var datesToString: [String] {
             get {
                 var stringDates:[String] = []
                 let formatter = NSDateFormatter()
                 formatter.dateFormat = "dd-MM-yyyy" //change the format as you wish
                 for date in dates {
                     stringDates.append(formatter.stringFromDate(date))
                 }
                 return stringDates
             }
        }

        for singleObject in objects {

            if let user = singleObject["sender"] as! PFObject {
                let first_name = user["first_name"] as! String
                let last_name = user["last_name"] as! String

                //Cast it to how you saved it. I can't see it from the picture so I assumed you saved it as NSData
                let picture = user["picture"] as! NSData
                picturesProfilesArray.append(picture)
                //once you fetch the user data, you can save it in a dictionary or however you want and later call it from cellForRowAtIndex
            }



            if let stringData = singleObject["message"] as? String {

                chats.timelinechatsData.append(stringData)

                if let messageDate = singleObject.createdAt {
                    chats.chatsDate.append(messageDate)
                }
            }

            //new
            if let messageObject = singleObject["messageObject"] as? String {
                message.messageObjts.append(messageObject)
            }

            //new senderNickname
            if let messageSender = singleObject["senderNickname"] as? String {
                message.messageSender.append(messageSender)
            }
        }

        //update self
        dispatch_async(dispatch_get_main_queue()) {

            self.timelineChatsDataArray += chats.timelinechatsData
            self.chatsDateArray += datesToString
            self.messageObjectArray += message.messageObjts
            self.messageSenderArray += message.messageSender

            self.tableView.reloadData()
        }
    }
})