Reputation: 313
I was trying to pull data from kimono I was encountered error. My kimono api's link = https://www.kimonolabs.com/api/8dfvxr3a?apikey=5747a54d5ca762895b474cc224943240
and Xcode error is
"thread 3: EXC_BREAKPOINT(code=EXC_1386_BPT,subcode0x0)"
How can I fix this error ?
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var countLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBOutlet weak var ara: UITextField!
@IBAction func getir(sender: AnyObject) {
araIMDB()
}
func araIMDB(){
var urlYol = NSURL(string: "http://www.kimonolabs.com/api/8dfvxr3a?apikey=5747a54d5ca762895b474cc224943240")
var oturum = NSURLSession.sharedSession()
var task = oturum.dataTaskWithURL(urlYol!){
data, response, error -> Void in
if (error != nil){
println(error)
}
var jsonError : NSError?
var jsonSonuc = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &jsonError) as Dictionary<String, String>
if( jsonError != nil)
{
println(jsonError)
}
dispatch_async(dispatch_get_main_queue()){
self.titleLabel.text = jsonSonuc["count"]
}
}
task.resume()
}
Upvotes: 1
Views: 362
Reputation: 51911
The exception is thrown at as Dictionary<String, String>
, because your JSON is not flat dictionary. In your case, you should cast it to NSDictionary
or Dictionary<String,AnyObject>
using optional form of the type cast operator (as?
).
You don't need NSJSONReadingOptions.MutableContainers
, because you only read from the result.
jsonSonuc["count"]
is integer value, not string. you should cast it to Int
using as? Int
, then covert it to String
.
Try:
var jsonError: NSError?
var jsonSonuc = NSJSONSerialization.JSONObjectWithData(data, options: .allZeros, error: &jsonError) as? NSDictionary
if( jsonError != nil) {
println(jsonError)
}
else if let count = jsonSonuc?["count"] as? Int {
dispatch_async(dispatch_get_main_queue()){
self.titleLabel.text = String(count)
}
}
Upvotes: 1