jeudi 9 février 2017

Use a String variable as key when getting value from JSON in Swift

Vote count: 0

I am trying to get a value from a json key: value pair by using a String var, but it is not working. (I am using SwiftyJSON)

func getValue(key: String){
   let url = NSBundle.mainBundle().URLForResource("devices", withExtension: "json")
   let data = NSData(contentsOfURL: url!)
   let json = JSON(data: data!)

   let value = json[key].string
}

I am passing a string when calling the function, and using that value as the key, but it doesn't work. Any suggestions?

asked 10 secs ago

Let's block ads! (Why?)



Use a String variable as key when getting value from JSON in Swift

Aucun commentaire:

Enregistrer un commentaire