na83
na83

Reputation: 111

NSDictionary, (key:AnyObject, value:AnyObject) does not have a member name subscript

I am learning swift for IOS , while I was doing tour on youtube (Swift: Using External Databases and API's by Skip Wilson )for external database, I found the part:

service = PostService()
    service.getPosts {
        (response) in
        self.loadPosts(response["posts"]! as NSDictionary)
    }

func loadPosts(posts:NSDictionary) {
        for post in posts {
            var id = (post["Post"]["id"] as String)
            var title = post["Post"]!["title"]! as String
            var author = post["Post"]!["author"]! as String
            var content = post["Post"]!["content"]! as String
            var postObj = Post(id: id, title: title, author: author, content: content)
           postsCollection.append(postObj)
        }
        dispatch_async(dispatch_get_main_queue()) {
            self.tableView.reloadData()
        }
    }

Originally the NSDictionary here was NSArray, but its not working since NSArray only takes int for key now, and PostService.getPost() return the type NSDictionary. So I changed it to NSDictionary.

However, all of the var id,title, author ,content has appear error

(key:AnyObject, value:AnyObject) does not have a member name subscript

it seems that I need to declare the key and value to be < String , String> ,but I am not sure now to do this. Blow is code for Postservice:

class PostService {

    var settings:Settings!

    init(){
        self.settings = Settings()
    }

    func getPosts(callback:(NSDictionary)->()) {
        println("get posts")
        request(settings.viewPosts,callback)
    }

    func request(url:String,callback:(NSDictionary)->()) {
        var nsURL = NSURL(string: url)
        println(callback)
        let task = NSURLSession.sharedSession().dataTaskWithURL(nsURL!) {
            (data,response,error) in
            var error:NSError?
            var response = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &error) as NSDictionary
                      callback(response)
        }
        task.resume()
    }
}

I know this could be a very simple and silly question, but I still cant figure it out after searching on google. What should I do? Any assistance will be highly appreciated.

Upvotes: 2

Views: 2741

Answers (2)

avismara
avismara

Reputation: 5149

The culprit here is Foundation's NSDictionary which simply doesn't have the subscript member. What you could instead try is Swift's Dictionary which does. If you are hell-bent on using NSDictionary you can use its objectForKey: API to get its value.

Upvotes: 2

mattdaw
mattdaw

Reputation: 121

On the line "for post in posts", the post variable is a tuple: (key:AnyObject, value:AnyObject). I'd suggest trying:

for (key, value) in posts {
    println(key)
    println(value)
}

... to inspect the data that's coming in to see if it's what you expect.

Upvotes: 0

Related Questions