ios - 奇怪且令人困惑的 Xcode 断点

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


}

最佳答案

问题在下面一行

 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客户端获取数据

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;

代替

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

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

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

关于ios - 奇怪且令人困惑的 Xcode 断点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27416542/

相关文章:

ios - 快速调用ViewController类方法

xcode - 当世界是物理体时,如何将世界置于位置中心?

Xcode 7 崩溃符号与 Mac App Store 上的 OS X 应用程序

Swift:防止玩家地址跟踪

swift - 访问同一文件中另一个 IBAction 中创建的变量

swift : Convert byte array into CIImage

iphone - 我怎样才能获得第二个mov播放时间

iphone - 如何使用 Xcode 从服务器读取 webService?

ios - XCode ibtool 命令寻找不存在的目录

ios - 保持函数之间的定义