奇怪而令人困惑的Xcode断点

时间:2014-12-11 06:17:34

标签: ios xcode swift

我一直在为Swift中的应用程序开发一个Web服务,但我刚测试它,它在swift_dynamicCastObjectObjCClassUnconditional的一行中给了我一个奇怪的断点。

发生在

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

当我按下我的按钮后,就会发生这种情况。

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()
    }


}

2 个答案:

答案 0 :(得分:0)

问题在下面

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

将其转换为

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

问题
从JSONObjectWithData返回的数据可以是NSArray或NSDictionary,并且您强制它为NSDictionary

更多细节
might be same problem

<小时/> 的修改
使用NSJSONSerialization.JSONObjectWithData的简单示例
Download project

<强>代码
使用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);
            }
        })

在上面的代码中,如果我写

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;

应用程序崩溃,错误与您在问题中提到的相同 enter image description here

<强>结论
使用JSOnSerialization
解析数据时出现错误 您从Web服务获得的数据肯定不是NSDictionary 尝试在日志中打印数据,看看你得到了什么

答案 1 :(得分:0)

您可能需要使用Optional Casting。如果您不确定数据返回,它是一种类型安全的。也可以使用AnyObject然后验证它是否真的是NSDictionary或只是一个零值。将nil传递给非nil var或非Optional var将导致App崩溃。