Bir URL'den json okuyorum ve tekrar (ObjectiveC ile aynı sorunu yaşadım) değerlerim uygulamanızı kilitledi. Dizeler ve Sayılarla ilgili problemim yok. Ben println (değer) yapabilirim, ama değeri bir UILabel'e atadığımda, çöker.Swift. Boş değerler ile json dosyalarına nasıl girebilirim?
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell? {
var regularTextCell:celda = celda(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
var cell:celda = NSBundle.mainBundle().loadNibNamed("celda", owner: self, options: nil)[0] as celda
cell.name.text = myJson["list"]![indexPath.row]!["name"] as String
cell.id.text = "id: " + String(myJson["list"]![indexPath.row]!["id"] as Int)
// THIS line crash because some values of adress are <null>
cell.address.text = myJson["list"]![indexPath.row]!["address"] as String
return cell
}
Sen bir örneğini görebilirsiniz
func jsonFromURL(jsonURL: String) -> Dictionary<String, AnyObject> {
var jsonNSURL: NSURL = NSURL(string: jsonURL)
let jsonSource: NSData = NSData(contentsOfURL: jsonNSURL)
var json = NSJSONSerialization.JSONObjectWithData(jsonSource, options:NSJSONReadingOptions.MutableContainers, error: nil) as Dictionary<String, AnyObject>
return json
}
... ve özel bir Cell içinde bir UILabel içine değerleri atamak için bu kodu:
Ben JSON okumak için bu yöntemi kullanmak JSON at: https://dl.dropboxusercontent.com/u/787784/example.json
Teşekkürler!
olası yinelenen (http: // stackoverflow.com/questions/24026609/det-a-null-value-in-nsdictionary) – Jeff