类型没有成员名称' objectForKey'和使用未解决的标识符

时间:2015-03-08 15:07:13

标签: ios swift parse-platform

使用swift / parse尝试在下表视图控制器中填充自定义单元格。 pfquery代码似乎没问题,但是当我尝试使用数据来填充cell.something.text以及应该返回的结果时,我收到错误,指示该类型没有名为'objectForKey'的成员并且使用未解决的标识符。这些错误特别发生在覆盖func tableView(tableView..cellForRowAtIndexPath ....

下)
import UIKit

class TimeLineTableViewController: UITableViewController {





    var timelineData:NSMutableArray = NSMutableArray()

    override init(style: UITableViewStyle) {
        super.init(style: style)
        // Custom initialization
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }



    func loadData(){
        timelineData.removeAllObjects()
        //let predicate = NSPredicate(format: PFuser = PFUser.current)
        var findTimelineData:PFQuery = PFQuery(className: "event")
        //findTimelineData.whereKey(PFUser.self, equalTo: PFUser.currentUser())
        findTimelineData.findObjectsInBackgroundWithBlock{
            (objects: [AnyObject]!, error: NSError!) -> Void in
            if error == nil {

                // The find succeeded.
                println("Successfully retrieved \(objects.count) scores.")
                // Do something with the found objects
                if let objects = objects as? [PFObject] {
                    for object in objects {
                        self.timelineData.addObject(object)
                        println(object.objectId)
                    }
                    let array:NSArray = self.timelineData.reverseObjectEnumerator().allObjects
                    self.timelineData = array as NSMutableArray
                    self.tableView.reloadData()
                }
            } else {
                // Log details of the failure
                println("Error: \(error) \(error.userInfo!)")

            }
        }

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Potentially incomplete method implementation.
        // Return the number of sections.
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete method implementation.
        // Return the number of rows in the section.
        return timelineData.count
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell:TimeLineTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TimeLineTableViewCell

        let event:PFObject = self.timelineData.objectAtIndex(indexPath.row) as PFObject

        cell.eventLabel.alpha = 0
        cell.dateLabel.alpha = 0
        cell.minutesLabel.alpha = 0

        cell.eventLabel.text = Category.objectForKey("content") as String
        cell.minutesLabel.text = duration.objectForKey


        var dataFormatter:NSDateFormatter = NSDateFormatter()
        dataFormatter.dateFormat = "yyyy-MM-dd HH:mm"
        cell.dateLabel.text = dataFormatter.stringFromDate(category.createdAt)

        var findRecorder:PFQuery = PFUser.query()
        findRecorder.whereKey("objectId", equalTo: event.objectForKey(user).objectId)

        findRecorder.findObjectsInBackgroundWithBlock{
            (objects:[AnyObject]!, error:NSError!)->Void in
            if error == nil{
                let user:PFUser = (objects as NSArray).lastObject as PFUser


                UIView.animateWithDuration(0.5, animations: {
                    cell.eventLabel.alpha = 1
                    cell.dateLabel.alpha = 1
                    cell.minutesLabel.alpha = 1
                })
            }
        }


        return cell
    }

    /*
    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return NO if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            // Delete the row from the data source
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return NO if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

}

1 个答案:

答案 0 :(得分:0)

您的问题是您使用的代码中没有初始化的变量:

cell.minutesLabel.text = duration.objectForKey
                            ^
cell.dateLabel.text = dataFormatter.stringFromDate(category.createdAt)
                                                      ^

您永远不会在代码中初始化durationcategory。所以你无法访问它。您首先需要初始化它。

此外,我不确定,但看起来您不会导入Parse框架(也许您可以,但它不在您提供的代码中)

所以你需要先导入它:

import Parse