Reputation: 1032
Since updating the latest Xcode, I've started to get the following error occurring "Ambiguous use of Subscript" related to this piece of code;
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("postsCell") as! CustomTableViewCell!
if cell == nil {
cell = CustomTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "postsCell")
}
let dict:NSDictionary = arrPosts[indexPath.row]
if let postResourceName = dict["resource_name"]![0] as! String? where !postResourceName.isEmpty {
cell?.customPostTitle?.text = String(htmlEncodedString: postResourceName)
} else if let postTitle = dict["title"]!["rendered"] as? String {
cell?.customPostTitle?.text = String(htmlEncodedString: postTitle)
}
Specifically on this line;
if let postResourceName = dict["resource_name"]![0] as! String? where !postResourceName.isEmpty {
I'm quite new to Swift, I believe this relates to the lack of detail around a variable type which is why the error is being thrown. But I'm unsure as to what the code should be.
Any pointers?
Regards, Michael
Upvotes: 0
Views: 494
Reputation: 4532
Swift compiler gives you that error because is does not know what objects do you have as values
in you dictionary, so when you do dict["resource_name"]![0]
you want to access the first item of the array contained in the dictionary as a value
. But you never tell that your values are arrays so the type of the values
is AnyObject
. You could specify that you have Array
in your dictionary by replacing
let dict:NSDictionary = arrPosts[indexPath.row]
with
let dict = arrPosts[indexPath.row] as [String:[String]]
Upvotes: 0