ios - Objective-C Conversion Errors -


i have project working on. project uses afnetworking 2.0 youtube gdata channel , load uitableview. there tutorial saw jared davidson showed me how so. great problem was in objective - c , project in swift. trying convert code swift have stumbled upon problem.

i tried converting this,

self.post = self.posts[@"data"][@"items"]; 

to in swift,

self.post = self.posts["data"]["items"] nsmutabledictionary 

but error says...

'anyobject?' not have member named 'subscript'

i have tried numerous ways fix still don't know how it. appreciated

thanks !

accessing dictionary returns optional since key may not exist.. how compiler parses it:

  1. call posts. returns nsdictionary.

  2. call subscript on returned object, argument of "data." returns optional anyobject.

  3. call subscript on returned anyobject?, argument of 'data'. causes error, because need unwrap it.

you need change to:

self.post = self.posts["data"]!["items"] nsmutabledictionary 

now happens:

  1. call posts. returns nsdictionary.

  2. call subscript on returned object, argument of "data." returns optional anyobject.

  3. unwrap optional. returns anyobject (not optional).

  4. call subscript on returned anyobject, argument of 'data'.

  5. cast nsmutabledictionary. believe cast unwraps it.

  6. call setpost result.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -