iOS和Parse抛出异常:"意外发现nil"

时间:2015-08-14 21:56:24

标签: ios swift parse-platform xcode6

我在Xcode 6.4上做了一些iOS应用程序,我使用Parse作为后端,一切都很顺利,直到我尝试将Parse得到的消息从对象添加到凝视数组。

错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

事情是我不知道它在谈论什么是零,因为我已经解开了所有的价值观,或者至少我是这么认为的,帮助?

代码:

var mensajes:[String]!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    println("\(PFUser.currentUser()!.username!)")
    menuLabel.text =  "Bienvenido \(PFUser.currentUser()!.username!)"
    /*
    var query = PFQuery(className: "Alumnos")

    query.whereKey("nombre", hasSuffix: "1")

    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in
        if error == nil {
            println("No error")
            println("we have \(objects!.count)")

            if let object = objects as? [PFObject] {
                for obj in object {
                    println("\(obj.objectId) ----")
                    println(obj)
                    println(obj["nombre"] as! String + "*****")
                }
            }
        } else {

            println("a misterious error has appeared \(error!) \(error!.description)")
        }

    } */

    var avisosQuery = PFQuery(className: "Alumnos")

    if let papa = PFUser.currentUser() {
        avisosQuery.whereKey("userId", equalTo: papa)

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

            if error == nil {

                println("No Error we have \(alumnos?.count) students")

                var grupos: PFObject? = nil

                if let obj = alumnos as? [PFObject] {
                    for alum in obj {
                        println(alum["nombre"])
                        //println(alum["grupoId"])
                        //println(alum)
                        grupos = alum["grupoId"] as? PFObject
                        println(grupos!)


                        var secondQuery = PFQuery(className: "Avisos")

                        secondQuery.whereKey("grupoId", equalTo: grupos!)

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

                            if error == nil {

                                println("No error we are home free \(avisos?.count)")

                                if avisos?.count > 0 {

                                    if let avisoArray = avisos as? [PFObject] {
                                        for av in avisoArray {
                                            println(av["texto"]!)

                                            if let msg: AnyObject = av["texto"] {

                                                println(msg)
                                                self.mensajes.append(msg as! String)

                                            }
                                        }
                                    }
                                }
                            } else {

                                println("something got busted, mate")
                            }
                        }
                    }
                }
            } else {

                println("we screw something up")
            }
        }
    }

}

我和Parse在学习阶段,为什么这是在viewDidLoad()中我只是尝试一些事情。

1 个答案:

答案 0 :(得分:1)

if let papa = PFUser.currentUser() 
     

问题出在那一行我们没有得到用户ID所以从技术上来说你正在寻找一个空值

正确的代码:

  

让papa = PFUser.currentUser()。objectId

试试吧