1

I've been working on a web service for my application in Swift, but I just tested it and it gave me a weird breakpoint at a line in swift_dynamicCastObjectObjCClassUnconditional.

It occurs at

0x104bbb7a2:  nopw   %cs:(%rax,%rax) and says "Thread 1: EXC_BREAKPOINT (code=EXC_l386_BPT, subcode=0x0)"

It occurs when I after I press my button.

import UIKit

class ViewController: UIViewController, NSURLConnectionDelegate {

    lazy var data = NSMutableData()

    @IBOutlet weak var usernameTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!
    @IBOutlet weak var activityIndicator: UIActivityIndicatorView!

    @IBAction func attemptLogin(sender: UIButton) {
        if(usernameTextField.text == "" || passwordTextField.text == "") {
            var alert = UIAlertController(title: "Error", message: "Invalid Credentials", preferredStyle: UIAlertControllerStyle.Alert)
            alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Default, handler: nil))
            self.presentViewController(alert, animated: true, completion: nil)
        } else {
            attemptConnection(usernameTextField.text, password: passwordTextField.text)
        }
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        var tapBackground: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard:")
        tapBackground.numberOfTapsRequired = 1;
        self.view.addGestureRecognizer(tapBackground)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func dismissKeyboard(sender: AnyObject) {
        self.view.endEditing(true)
    }

    func attemptConnection(username: String, password: String){
        let urlPath: String = "http://notmywebsite.com/getusers.php?username=" + username + "&password=" + password
        var url: NSURL = NSURL(string: urlPath)!
        var request: NSURLRequest = NSURLRequest(URL: url)
        var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)!
        connection.start()
        activityIndicator.startAnimating()
    }

    func connection(connection: NSURLConnection!, didReceiveData data: NSData!){
        self.data.appendData(data)
    }

    func connectionDidFinishLoading(connection: NSURLConnection!) {
        var err: NSError
        var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
        println(jsonResult)
        activityIndicator.stopAnimating()
    }


}
Kampai
  • 22,848
  • 21
  • 95
  • 95
Jeffrey
  • 1,271
  • 2
  • 15
  • 31
  • 1
    its shows that your application crashing at that place – Jageen Dec 11 '14 at 06:18
  • show me line where you get that breakpoint – Jageen Dec 11 '14 at 06:19
  • Add exception break point as i show in this image and let me know which line is crashing https://www.dropbox.com/s/rq4o7vdldrsyhge/Screen%20Shot%202014-12-11%20at%2011.55.50%20am.png?dl=0 – Jageen Dec 11 '14 at 06:27

2 Answers2

0

Problem is in line below

 var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary

convert it as

 var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil)

Problem is
Data which is return from JSONObjectWithData can be NSArray or NSDictionary and you are forcing it to be a NSDictionary

More detail
might be same problem


Edit
Simple example of working with NSJSONSerialization.JSONObjectWithData
Download project

Code
Fetch data using rest client

NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue(), completionHandler: { (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
            if data.length > 0
            {
                var greeting :NSDictionary! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.allZeros, error: nil) as NSDictionary;

                let id  = greeting["id"] as Int;
                let responce  = greeting["content"] as NSString

                self.lblId.text = "Id : \(id)"
                self.lblResponce.text = "Responce : \(responce)"

                myButton.setTitle("Fetch data", forState: UIControlState.Normal)

                println(greeting);
            }
        })

In above code if i write

var greeting :NSArray! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.allZeros, error: nil) as NSArray;

Insted of

 var greeting :NSDictionary! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.allZeros, error: nil) as NSDictionary;

Application crash with error same as you mention in your question
enter image description here

Conclusion
Error is in the line while you parsing data using JSOnSerialization
Data you get from web service is not NSDictionary for sure.
Try to print data in log and see what you get

Community
  • 1
  • 1
Jageen
  • 6,345
  • 2
  • 37
  • 56
  • did you read the answers given in the link ? http://stackoverflow.com/questions/24065536/downloading-and-parsing-json-in-swift – Jageen Dec 12 '14 at 03:50
0

You should probably need to use Optional Casting. It is a type safe if you're not sure of your data returns. Also use AnyObject then verify if it is really an NSDictionary or just a nil value. Passing nil to a non-nil var or non-Optional var will crash your App.

eNeF
  • 3,241
  • 2
  • 18
  • 41