-1

I'm trying to write a class for parsing xml. I want to show Alert when some error occurs. Here is my code:

Parse.swift

func parserXml(xmlUrl:String,completionHandler:([(staID: String, staName: String, ava: String, unava: String)]->Void)?)->Void{

    self.paraserCompletionHandler = completionHandler
    let request = NSURLRequest(URL: NSURL(string: xmlUrl)!)
    let urlConfig = NSURLSessionConfiguration.defaultSessionConfiguration()
    urlConfig.timeoutIntervalForRequest = 30
    urlConfig.timeoutIntervalForResource = 60
    let urlSession = NSURLSession(configuration: urlConfig, delegate: self, delegateQueue: nil)

    let task = urlSession.dataTaskWithRequest(request, completionHandler: {(data,response,error)->Void in
        if error != nil{
            print(error?.localizedDescription)
            if (error?.code == NSURLErrorTimedOut || error?.code == NSURLErrorNotConnectedToInternet){
                let vc = ViewController()
                vc.alertView()
            }
        }else{
            let parser = NSXMLParser(data: data!)
            parser.delegate = self
            parser.parse()
        }            
    })
    task.resume()
}

ViewController.swift

func alertView(){
    var alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)
    var okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default)
    self.presentViewController(alertController, animated: true, completion: nil)
}

When I run my app, my app crash and show error

fatal error: unexpectedly found nil while unwrapping an Optional value

EDIT: Xcode Stop at

self.presentViewController(alertController, animated: true, completion: nil)

I guess the reason is alertController in ViewController.swift is called by Parse.swift

can anyone resolve this question? thx

Keith
  • 23
  • 5

1 Answers1

0

Throw a custom exception in Parse.swift and catch it in ViewController.swift. You could create a custom class (NSException) where you establish this exception and implement it into both.

Here's help handling the exception if it is unfamiliar.

if error {
    print(error?.localizedDescription)
    if (error?.code == NSURLErrorTimedOut ||
        error?.code == NSURLErrorNotConnectedToInternet){
        @throw [[CustomException alloc] initWithName:@"Title"
                                              reason:@"Message"
                                            userInfo:nil];
    }
} else{
            let parser = NSXMLParser(data: data!)
            parser.delegate = self
            parser.parse()
        }

In ViewController try() calling parserXml(...) then in the catch() that's when you give the alert this way you can throw multiple exceptions in parserXml(...) which would make this next statement perfect if you were have multiple error types you'd like to display.

catch (YourCustomException *ce) {
    alertController = UIAlertController(title: ce.name,
                                      message: ce.message,
                               preferredStyle: .Alert)
}
Community
  • 1
  • 1
Jab
  • 26,853
  • 21
  • 75
  • 114
  • 1
    I use another way to do what I want, but I think your answer is also useful. thanks for your help :) – Keith Jan 25 '16 at 17:09