如何从解析中获取createdAt并追加到数组 - swift

时间:2015-09-28 21:06:43

标签: swift parse-platform

我成功从parse中获取值并将它们存储到数组中

var dataSource: [PFObject] = [] 

当我从parse追加createdAt值时,这是一个NSDate数据类型。

Cannot convert value of type 'NSDate!' to expected argument type 'PFObject'

使用此代码

self.dataSource.append(object.createdAt as NSDate!)

这里是整个代码。

override func tableView(tableView: UITableView,      cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellIdentifier = "JobsTVCellIdentifier"
    let cell:JobsTVCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as? JobsTVCell

    let query = PFQuery(className: "Post")
    if let currentUser = PFUser.currentUser() {
        query.whereKey("user", equalTo: currentUser)
    }
    query.orderByDescending("createdAt")
    let objects = query.findObjects()
    for object in (objects as? [PFObject])!{
        //print(object.objectId)
        self.dataSource.append(object)
        self.dataSource.append(object.createdAt as NSDate!)
//            let genderValues = object["gender"] as! String
//            print(genderValues)
//            let dateValues = object.createdAt
//            var dateFormatter = NSDateFormatter()
//            dateFormatter.dateFormat = "yyyy'-'MM'-'dd'"
//            var strDate = dateFormatter.stringFromDate(dateValues!)
//            cell?.createdAt.text = strDate
        print(dataSource)
        let itemArr:PFObject = self.dataSource[indexPath.row] as! PFObject
        cell?.genderTextfield.text = itemArr["gender"] as! String
        cell?.occupationTextfield.text = itemArr["occupation"] as! String

    }
    return cell!
}

1 个答案:

答案 0 :(得分:0)

您正尝试将NSAate的createdAt属性追加到PFObjects数组中,但这并不起作用。

您有两个选择:

1)创建NSDates数组,如下所示:

var dataSource: [NSDate] = []

并将该属性附加到它。

2)将整个对象附加到PFObject数组,然后读取createdAt属性:

dataSource.append(object)
let createdAt = dataSource["createdAt"]