2

So I have an array as the value of one of my keys in a dictionary and I want to get it to set it as the text of a UITableViewCell. How do I get it? I've been coding in Objective-C and I'm starting to learn Swift.

var content: [String: Any] = ["content": ["North America", "Europe West", "Europe Nordic & East", "Oceania", "Russia", "Turkey", "Brazil", "Latin America North", "Latin America South"], "footer": "Select the League of Legends region for this app to use."]

autoreleasepool { () -> () in
    var temp = self.content["content"]
    cell.textLabel?.text = temp[indexPath.row] as String
}

This returns me 2 errors '_??' is not convertible to 'int' and '<<error type>>?' is not convertible to 'int'

4
  • what's your datatype of temp Commented Jun 13, 2015 at 5:34
  • Show me the values of dictionary Commented Jun 13, 2015 at 5:35
  • @jacky should it inherit the data type of self.content["content"]? Commented Jun 13, 2015 at 5:40
  • @SohilR.Memon var content: [String: Any] = ["content": ["North America", "Europe West", "Europe Nordic & East", "Oceania", "Russia", "Turkey", "Brazil", "Latin America North", "Latin America South"], "footer": "Select the League of Legends region for this app to use."] Commented Jun 13, 2015 at 5:41

3 Answers 3

3

You should declare your dictionary values as AnyObject. You should also use if let to unwrap your optionals:

let content: [String:AnyObject] = ["content": ["North America", "Europe West", "Europe Nordic & East", "Oceania", "Russia", "Turkey", "Brazil", "Latin America North", "Latin America South"], "footer": "Select the League of Legends region for this app to use."]

if let temp = content["content"] as? [String] {
     cell.textLabel!.text = temp[indexPath.row]
}
Sign up to request clarification or add additional context in comments.

Comments

2

You have to convert your temp to Array for getting data.

var temp: Array = self.content["content"] as! Array
cell.textLabel?.text = temp[indexPath.row] as! String

It will solve your error.

2 Comments

'Any?' is not convertible to 'Array<T>' That might be because I didn't set the array value type when I declared content?
var content: [String: AnyObject] do this casting
2

This is because it doesn't know what's kind of type in your temp. Swift is an type safe language.

I write this in playground:

var content: [String: AnyObject] = ["content": ["North America", "Europe West", "Europe Nordic & East", "Oceania", "Russia", "Turkey", "Brazil", "Latin America North", "Latin America South"], "footer": "Select the League of Legends region for this app to use."]

var temp:NSArray = content["content"] as! NSArray

let cellString = temp[0] as! String

print(cellString)

enter image description here

1 Comment

its habit from when I used to aggressively optimize memory usage and clear stuff immediately after I finish using it.

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.