Swift:如何获取 Parse 的多个用户名和照片

Posted

技术标签:

【中文标题】Swift:如何获取 Parse 的多个用户名和照片【英文标题】:Swift: how to get Parse's many user's name and photo 【发布时间】:2015-11-09 10:12:38 【问题描述】:

我的消息应用程序上有一群用户,正在获取我的“消息”类.

我只想在 tableView 中显示包含在“sender”列中的“messages”类中的用户名,其中包含指向 PFUsers 的指针(我需要“first_name”、“last_name”、“profile_picture”)

我的用户类

我的消息类

更新!

不知道问题出在哪里,如果我向下转换某些内容,则必须更改其他内容。这里是更新的代码:

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'

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

【问题讨论】:

【参考方案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()
        
    
)

【讨论】:

再次感谢 Lukas,看看更新后的问题,我将字符串更改为 NSDate 但收到警告 (也许我没有解释清楚,但我的意图是从“发件人”指针中检索名称和图像,而不是发件人昵称,这会改变您的解决方案吗? 取决于您的实体的设置方式。你能发布更多的揭示图片吗 添加了更多图片,让问题更清晰,希望对您有所帮助! 我想我现在回答了你的问题

以上是关于Swift:如何获取 Parse 的多个用户名和照片的主要内容,如果未能解决你的问题,请参考以下文章

IOS 如何使用 Parse Swift 获取每个用户的个人资料图片

如何使用 Parse (swift) 邀请用户加入多人游戏会话

使用 Swift 和 Parse 获取 Facebook UserData 时出现致命错误

使用 Parse 和 Swift 的一个键上的两个查询约束

如何在我的应用程序中阻止用户(swift + parse)

如何使用 swift 从 Parse 上的用户指针中检索数据