ios - 从 Parse 中检索信息

标签 ios swift parse-platform

我正在尝试构建一个聊天应用程序,但是这段代码有问题:

func loadData() {

        let FindTimeLineData: PFQuery = PFQuery(className: "Message")
        FindTimeLineData.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, NSError) -> Void in

            self.MessagesArray = [String]()

            for MessageObject in objects {

                let messageText: String? = (MessageObject as! PFObject) ["Text"] as? String

                if messageText != nil {

                    self.MessagesArray.append(messageText!)

          }
        }
      }
    }

我需要从 Parse 中检索数据,但是 .findObjectsInBackgroundWithBlock 方法告诉我它无法将 AnyObject 类型的值转换为 Void in。我该如何解决这个问题?提前致谢。

最佳答案

试试这样:

var query = PFQuery(className: "Message")
query.findObjectsInBackgroundWithBlock {
    (remoteObjects: [PFObject]?, error: NSError?) -> Void in

    if error == nil {
        print("Retrieved \(remoteObjects!.count) messages from server.")
        self.MessagesArray = [String]()     // By convention, you should name this "messagesArray" instead, and initialize it outside this method

        for messageObject in remoteObjects {

            if let messageText: String? = messageObject["Text"] as? String {
                self.messagesArray.append(messageText)
            }
        }
    } else {
         print("Error: \(error!) \(error!.userInfo)")
    }
}

(没有正确校对,但你应该能够让它发挥作用)

关于ios - 从 Parse 中检索信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34373171/

相关文章:

ios - 解析远程通知和时区

ios UITableViewCell 总是多一个字符,为什么?

ios - 在 'Edit' 模式下向 UITableView 添加部分

ios - 如何将类型 'UIImage?' 的值分配给类型 'UIView?'

swift - SpriteKit 中数字变化的动画

ios - setObjectForKey : exception: object cannot be nil

ios - 使用 SOAP 网络服务时出现 "No Action header was found"错误消息

ios - 获取位于 UITableView 中的 UIButton 的标题

ios - 位置权限弹出窗口不使用 CFBundleSpokenName

ios - FB 登录后解析 session token 无效