简体   繁体   中英

how to fetch createdAt from parse and append to an array - swift

I successfully fetched values from parse and storing them into an array

var dataSource: [PFObject] = [] 

and when I append createdAt values from parse which is a NSDate data type.

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

with this code

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

here's the whole code.

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!
}

You are trying to append createdAt property which is NSDate to array of PFObjects, and that doesn't work.

You have 2 options:

1) create array of NSDates like so:

var dataSource: [NSDate] = []

and append that property to it.

2) append whole object to PFObject array and then read createdAt property:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM