0

I'm new to swift and I am having difficulty parsing an array the array looks like this

MyArray [ [0]9keyValuePairs [1]9keyValuePairs ]

I would like to add "MyArray.(0).ValueForKey:"Name" " to a UITableViewCell, but I can't quite figure out the correct syntax.

2

1 Answer 1

1

How is your array declared? If it is a array of something generic, ie [AnyObject] then you need to tell the type checker that the object in the array is a dictionary by casting it, otherwise you wont be able to access it as a dictionary.

If it is explicitly declared as an array of dictionaries ie [[String:AnyObject]] , then you just need to access the element in the array that you want, and then access the dictionary element you are interested in.

array[0] //how to get something out of an array
dictionary[key] //how to get something out of a dict
array[0][key] //how to get something out of an array of dicts

// if your array contents need to be cast, safely cast it using optional unwrapping 
if let dict = array[0] as? [String:AnyObject] { 
    dict[key]
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this is the exactly what I was missing. Much appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.