I am trying to get values from an array that I get from a JSON web request. But I can't get the valueForKey function to work, so I can apply String to the label. The example below searches for application APIs for software. As a test, I want to be able to apply the "trackName" key to a UILabel, but all I'm trying to do is either crash or return nil.
Here is my code
func searchFunction(searchQuery: NSString) { var url : NSURL = NSURL.URLWithString("https://itunes.apple.com/search?term=\(searchQuery)&media=software") var request: NSURLRequest = NSURLRequest(URL:url) let config = NSURLSessionConfiguration.defaultSessionConfiguration() let session = NSURLSession(configuration: config) let task : NSURLSessionDataTask = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in var newdata : NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary var info : NSArray = newdata.valueForKey("results") as NSArray var name: String? = info.valueForKey("trackName") as? String
The comments next to the variables explain what happens to each of them. Can anyone explain how I can convert valueForKey ("trackName") to a string that can be applied to a label?
Thanks!
ios swift
Dookieman
source share