xcode map view返回nil致命错误

时间:2015-05-27 12:37:36

标签: ios swift mkmapview viewcontroller

我试图在用户收到推送通知后向我的mapView添加新注释。但这总是会返回致命错误:在展开Optional值时意外地发现了nil。请注意,我在项目中使用了相同的代码(在相同的视图控制器中),没有错误。我也打印出了值,所以我确定问题出在视图控制器上。

当用户收到推送通知时,我从视图控制器调用一个方法,从app delegate didreceiveRemoteNotification处理通知。方法被成功调用并且数据正确传递但是应用程序总是在添加注释时崩溃。

这是我尝试过的; 我尝试在此方法中本地声明注释,该方法由应用程序委托调用,但仍然不起作用。

任何帮助,请

以下是代码Appdelegate

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    PFPush.handlePush(userInfo)
    if application.applicationState == UIApplicationState.Inactive {
        PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
    }



        // Create a pointer to the Photo object
    if let objectID: String = userInfo["extractThis"] as? String{
        println(objectID)

        //let rootViewController = ViewController.self
        //rootViewController.appDelegateCall(objectID as! AnyObject)
        println("I am calling the method in viewcontroller")
        var vc = ViewController()
        vc.appDelegateCall(objectID)

    }


}

以及从AppDelegate调用的视图控制器方法

func appDelegateCall(abbey : String){

    println("I was called by the Appdelegate with value \(abbey)")
    var query = PFQuery(className:"drops")
    println("break point")
    query.getObjectInBackgroundWithId(abbey) {
        (newDrop: PFObject?, error: NSError?) -> Void in
        println("break point 2 with abbey as \(abbey)")
        if error != nil {
            println("no object found for \(abbey)")
        }

        else if let object = newDrop {

            var wrapstatus: AnyObject! = object.objectForKey("status")
            var status: String = wrapstatus as! String
            var objectID = object.objectId!
            var bar: AnyObject! = object.objectForKey("latitude")
            let noteLat = (bar as! NSString).doubleValue
            var bar2: AnyObject! = object.objectForKey("longitude")
            let noteLong = (bar2 as! NSString).doubleValue
            var dropperName: AnyObject! = object.objectForKey("dropper")
            var dropTime: AnyObject! = object.objectForKey("when")
            var dropedNote: AnyObject! = object.objectForKey("note")
            var receiver: AnyObject! = object.objectForKey("receiver")
            var latitude:CLLocationDegrees = noteLat
            var longitude:CLLocationDegrees = noteLong
            let noteLocation = CLLocationCoordinate2DMake(noteLat, noteLong)
            var locationP = CLLocation(latitude:noteLat, longitude:noteLong)


            //add coordinates to array
            MyVariables.noteLocation += [locationP]

            var newNote = CustomPointAnnotation()
            newNote.coordinate = noteLocation
            newNote.title = "\(dropperName)"
            newNote.subtitle = "\(dropTime!) \n from: \(dropperName) \n \(dropedNote) \n\(objectID)\n\(status)"

            if status == "unread"{

                newNote.imageName = "cNote.png"
            }

            else{

               newNote.imageName = "ownNote.png"
            }

           // the app always crash here
            self.mapView.addAnnotation(newNote)
        }
    }
}

0 个答案:

没有答案