我的用户类我的消息类更新!不知道问题出在哪里,如果我向下投射某些东西,则必须更改其他东西.这里是更新的代码:findTimeLineDataQuery.includeKey("sender")findTimeLineDataQuery.findObjectsInBackgroundWithBlock({(对象:[AnyObject]?,错误:NSError?)在if let objects = objects where error == nil {var chats:(timelinechatsData: [String], chatsDate: [NSDate]) = ([], [])//日期可能是NSDatevar message: (messageObjts: [String], messageSender: [String]) = ([], [])var dateToString: [字符串] {得到 {var stringDates:[String] = []让格式化程序 = NSDateFormatter()formatter.dateFormat = "dd-MM-yyyy"//根据需要更改格式日期中的日期{stringDates.append(formatter.stringFromDate(date))}返回字符串日期}}对于对象中的 singleObject {if let user = singleObject["sender"] as?PFObject {让 first_name = user["first_name"] 作为!细绳让 last_name = user["last_name"] 作为!细绳//将其转换为您保存的方式.我从图片中看不到它,所以我假设你将它保存为 NSData让图片=用户[图片"]作为!NSDataself.picturesProfilesArray.append(图片)//一旦您获取用户数据,您就可以将其保存在字典中,或者您想保存它,然后从 cellForRowAtIndex 调用它}if let stringData = singleObject["message"] as?细绳 {chats.timelinechatsData.append(stringData)如果让 messageDate = singleObject.createdAt {chats.chatsDate.append(messageDate!)}}//新的如果让 messageObject = singleObject["messageObject"] 作为?细绳 {message.messageObjts.append(messageObject)}//新的发件人昵称if let messageSender = singleObject["senderNickname"] as?细绳 {message.messageSender.append(messageSender)}}//更新自己dispatch_async(dispatch_get_main_queue()){self.timelineChatsDataArray += chats.timelinechatsDataself.chatsDateArray += 日期ToStringself.messageObjectArray += message.messageObjtsself.messageSenderArray += message.messageSenderself.tableView.reloadData()}}})变化所以我的var chatsDateArray : [NSDate] = []和let dateFormatter = NSDateFormatter()dateFormatter.dateFormat = "MM-dd-yyyy HH:mm"让 useDate = dateFormatter.stringFromDate(self.chatsDateArray[indexPath.row])cell.dateMessageLabel.text = useDate应该变成 var chatsDateArray : [String] = []让 dateFormatter = NSDateFormatter()dateFormatter.dateFormat = "MM-dd-yyyy HH:mm"让 useDate = String(self.chatsDateArray[indexPath.row])cell.dateMessageLabel.text = useDate但这会导致:不可变值first_name"的初始化从未被使用过;考虑替换为_"的赋值或将其删除从未使用过不可变值last_name"的初始化;考虑替换为_"的赋值或将其删除这通过将 var chat (chatsDate) 改回字符串而消失,但这会导致 无法将NSDate"类型的值转换为预期的参数类型String"在if let messageDate = singleObject.createdAt {chats.chatsDate.append(messageDate!)}}解决方案可以使用'includeKey'来访问相关类的指针值findTimeLineDataQuery.includeKey("sender")findTimeLineDataQuery.findObjectsInBackgroundWithBlock({(对象:[AnyObject]?,错误:NSError?)在if let objects = objects where error == nil {var chats:(timelinechatsData: [String], chatsDate: [NSDate]) = ([], [])//日期可能是NSDatevar message: (messageObjts: [String], messageSender: [String]) = ([], [])var dateToString: [字符串] {得到 {var stringDates:[String] = []让格式化程序 = NSDateFormatter()formatter.dateFormat = "dd-MM-yyyy"//根据需要更改格式日期中的日期{stringDates.append(formatter.stringFromDate(date))}返回字符串日期}}对于对象中的 singleObject {if let user = singleObject["sender"] as!PFObject {让 first_name = user["first_name"] 作为!细绳让 last_name = user["last_name"] 作为!细绳//将其转换为您保存的方式.我从图片中看不到它,所以我假设你将它保存为 NSData让图片=用户[图片"]作为!NSData图片配置文件Array.append(图片)//一旦您获取用户数据,您就可以将其保存在字典中,或者您想保存它,然后从 cellForRowAtIndex 调用它}if let stringData = singleObject["message"] as?细绳 {chats.timelinechatsData.append(stringData)如果让 messageDate = singleObject.createdAt {chats.chatsDate.append(messageDate)}}//新的如果让 messageObject = singleObject["messageObject"] 作为?细绳 {message.messageObjts.append(messageObject)}//新的发件人昵称if let messageSender = singleObject["senderNickname"] as?细绳 {message.messageSender.append(messageSender)}}//更新自己dispatch_async(dispatch_get_main_queue()){self.timelineChatsDataArray += chats.timelinechatsDataself.chatsDateArray += 日期ToStringself.messageObjectArray += message.messageObjtsself.messageSenderArray += message.messageSenderself.tableView.reloadData()}}})I have a bunch of users on my message app, fetching my "messages" class I need to get sender's first and last name and profile image from their Parse profile on my app in order to show them in each message on the tableView.I just want to show in tableView the name of users in class "messages" contained in the column "sender" wich contains pointers to PFUsers (of which I need "first_name", "last_name", "profile_picture")my users classmy message classupdate!can't get where is the problem, if I downCast something, something else must be changed. here the updated code: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() } }})changesso my var chatsDateArray : [NSDate] = []andlet dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "MM-dd-yyyy HH:mm" let useDate = dateFormatter.stringFromDate(self.chatsDateArray[indexPath.row]) cell.dateMessageLabel.text = useDateshould become var chatsDateArray : [String] = []let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "MM-dd-yyyy HH:mm" let useDate = String(self.chatsDateArray[indexPath.row]) cell.dateMessageLabel.text = useDatebut this causes:Initialization of immutable value 'first_name' was never used; consider replacing with assignment to '_' or removing itInitialization of immutable value 'last_name' was never used; consider replacing with assignment to '_' or removing itthis disappears by changing var chat (chatsDate) back to string, but this causes Cannot convert value of type 'NSDate' to expected argument type 'String'on if let messageDate = singleObject.createdAt { chats.chatsDate.append(messageDate!) } } 解决方案 You can use 'includeKey' to access pointer values of related classes 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() } }}) 这篇关于Swift:如何获取 Parse 的许多用户的姓名和照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-14 09:21