即时通过Parse使用swift对xcode进行使用,并且在之前为我工作的代码上保持同样的错误

时间:2015-05-24 09:41:18

标签: ios xcode swift parse-platform

我一直收到以下错误:

error

在我得到的日志中

unexpectedly found nil while unwrapping an Optional value

我遇到这个问题的原因是因为我之前运行了这个代码并且工作正常可以有人请帮助并告诉我代码中哪里出错了? 这是完整的代码

 import UIKit
import Parse


class HomePage: UITableViewController {

var images = [UIImage]()
var titles = [String]()
var imageFile = [PFFile]()
var voteCounter = [Int]()





override func viewDidLoad() {
    super.viewDidLoad()

    println(PFUser.currentUser())

    var query = PFQuery(className:"Post")

    var voteCount = PFObject(className: "Post")
    voteCount["voteCounter"] = voteCounter
    voteCount.saveInBackground()



    query.orderByDescending("createdAt")
    query.findObjectsInBackgroundWithBlock {(objects: [AnyObject]?, error: NSError?) -> Void in
        if error == nil  {

            println("Successfully retrieved \(objects!.count) scores.")

            for object in objects! {

                self.titles.append(object["Title"] as! String)

                self.imageFile.append(object["imageFile"] as! PFFile)

                self.tableView.reloadData()

            }
        } else {
            // Log details of the failure
            println(error)
        }
    }

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return titles.count


}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 500

}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var myCell:cell = self.tableView.dequeueReusableCellWithIdentifier("myCell") as! cell

    myCell.rank.text = "21"
    myCell.votes.text = "\(voteCounter)"
    myCell.postDescription.text = titles[indexPath.row]

    imageFile[indexPath.row].getDataInBackgroundWithBlock { (data, error) -> Void in

        if let downloadedImage = UIImage(data: data!) {

            myCell.postedImage.image = downloadedImage

        }
    }



    var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    myCell.postedImage.userInteractionEnabled = true;
    myCell.postedImage.addGestureRecognizer(swipeRight)


    var swipeLeft = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
    swipeRight.direction = UISwipeGestureRecognizerDirection.Left
    myCell.postedImage.userInteractionEnabled = true;
    myCell.postedImage.addGestureRecognizer(swipeLeft)

    return myCell

}


func respondToSwipeGesture(gesture: UIGestureRecognizer) {

        if let swipeGesture = gesture as? UISwipeGestureRecognizer {
        var voteCounter = 0
            switch swipeGesture.direction {
            case UISwipeGestureRecognizerDirection.Right:
                voteCounter += 1
                println("Swiped right")
            case UISwipeGestureRecognizerDirection.Left:
                voteCounter -= 1
                println("Swiped Left")
            default:
                break
            }
        }
    }

}

任何进一步的建议或评论都会对编程

相当新颖

1 个答案:

答案 0 :(得分:1)

试试这个。

query.findObjectsInBackgroundWithBlock {(objects: [AnyObject]?, error: NSError?) -> Void in

    if error == nil  {

        println("Successfully retrieved \(objects!.count) scores.")

        for object in objects! {
            if let dict = object as? [String: AnyObject] {
               if let title = object["Title"] as? String {
                  self.titles.append(title)
               }
               if let imgFile = object["imageFile"] as? PFFile {
                  self.imageFile.append(imgFile)
               }
            }

            self.tableView.reloadData()

        }
    } else {
        // Log details of the failure
        println(error)
    }
}