2016-08-11 62 views
1

我有問題,此代碼 -錯誤 - 線程1個exc_bad_instruction(代碼= exc_1386_invop子碼=爲0x0)

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


func get(){ 
    let url = NSURL(string: "http://www..php") 
    let data = NSData(contentsOfURL: url!) 
    values = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as!NSArray 

    tableView.reloadData() 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return values.count; 
} 

此錯誤 線程1個exc_bad_instruction(代碼= exc_1386_invop子碼=爲0x0)

+1

你可以分享你的數據,以便更好地理解。 –

+0

@Anupam米什拉 - FUNC得到(){ 設URL = NSURL(字符串: 「HTTP://www..php」) 讓數據= NSData的: 值=嘗試(contentsOfURL網址!)! NSJSONSerialization.JSONObjectWithData(數據!選項:NSJSONReadingOptions.MutableContainers)作爲NSArray的 tableView.reloadData() } FUNC的tableView(的tableView:UITableView的,numberOfRowsInSection部的:int) - >內部{ 返回values.count; } – hamid

+0

請在原始文章中添加您的代碼並將其格式化(cmd + k)。 – jbehrens94

回答

0

正如我可以看到你將從請求中解析JSON。

嘗試使用我的建議。

首先不要使用try!這是壞習慣嘗試正確處理異常。

//Catch error if it is son error. 
do { 
    //Parse JSON and handle exception. 
    let JSON = try NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions(rawValue: 0)) 
    guard let JSONDictionary :NSDictionary = JSON as? NSDictionary else { 
     print("Not a Dictionary") 
     return 
    } 
    print("JSONDictionary! \(JSONDictionary)") 
} 
catch let JSONError as NSError { 
    print("\(JSONError)") 
} 
1

試試這個 -

func get() 
{ 
if let url = NSURL(string: "https://www.hackingwithswift.com") { 
    do { 

     let JSONData = NSData(contentsOfURL: url) 
     do { 
      let JSON = try NSJSONSerialization.JSONObjectWithData(JSONData!, options:NSJSONReadingOptions(rawValue: 0)) 
      guard let JSONDictionary :NSDictionary = JSON as? NSDictionary else { 
       print("Not a Dictionary") 
       // put in function 
       return 
      } 
      print("JSONDictionary! \(JSONDictionary)") 
     } 
     catch let JSONError as NSError { 
      print("\(JSONError)") 
     } 
    } catch { 
     // contents could not be loaded 
    } 
} 
else 
    { 
    // the URL was bad! 
    } 
} 
相關問題