Swift - 'PFObject不能转换为NSString'

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Swift - 'PFObject不能转换为NSString'相关的知识,希望对你有一定的参考价值。

我一直在尝试打印在我的PFQuery中调用的viewWillAppear,但是我从这一行得到了错误:

cell?.textLabel?.text = message as NSString as String

这是tableView cellForRowAtIndexPath的代码:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellId: NSString = "cell"
    var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellId as String) as? UITableViewCell
    if (cell == nil) {
        cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellId as String)
    }
    let message = myMessagesArray![indexPath.row] as! PFObject
    cell?.textLabel?.text = message as NSString as String
    return cell!
}

这是PFQuery

 var query = PFUser.query()
    query!.orderByAscending("Messages")
    query!.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in
        if error == nil {
            println("Successfully retrieved (objects!.count) messages.")
            self.myMessagesArray = objects
            self.tableView?.reloadData()
        } else {
            println(error!.localizedDescription)
        }
    }
答案

使用此代码:

let message = myMessagesArray[indexPath!.row] as! PFObject
cell.textLabel?.text = message.objectForKey("Messages") as? String

以上是关于Swift - 'PFObject不能转换为NSString'的主要内容,如果未能解决你的问题,请参考以下文章