Dakata Dakata - 2 months ago 7
Swift Question

How to print error in Swift code?

I have the following code:

func marseJSON(Person: String)
{


let jsonData1 = NSData(contentsOfURL: NSURL(string: "\(label2)")!) as NSData!

var error : NSError?
let ReadableJSON1 = JSON ( data:jsonData1, options: NSJSONReadingOptions.MutableContainers, error: &error )

// print(error)

LabelText1.text = "There is a problem with the qr code! or simply error"


I have a function which reads a QR code, the problem is that this QR code should be always a link from my server. Would you please show me how can I display an error message into the LabelText1.text if the user does not scan the right QR code for my server.
I have tried by myself but I am new in swift and it seems that the syntax is wrong.

Answer

you can try UIAlertController:

   if (error != nil){

   let myAlert = UIAlertController(title: "Alert", message: error?.localizedDescription, preferredStyle: .Alert) //you can change message to whatever you want.
                    let okAction = UIAlertAction(title: "Ok", style: .Default, handler: nil)
   myAlert.addAction(okAction)
   self.presentViewController(myAlert, animated: true, completion: nil)

   return
       }

Hope it helps. :)

Comments