0

I have one array of dictionary and I am trying to do is get first object from array and append in my secondArray. Dictionary is [String: AnyObject] and array is of String. Here is my code in didSelectRowAtIndexPath:

let myDictionary: [String: AnyObject] = dataArray[indexPath.row] as! [String : AnyObject]
selectedArray.append(myDictionary)

But I am getting error :

Cannot convert value of type '[String : AnyObject]' to expected argument type 'String'.

How can I add dictionary in array?

2
  • Maybe you have typo in the error citation... Please correct Commented Aug 25, 2016 at 8:52
  • How you declared selectedArray? Try to declare it as:- var selectedArray : NSMutableArray = NSMutableArray() Commented Aug 25, 2016 at 8:53

1 Answer 1

4

You need to declare your array of dictionary type [[String: AnyObject]] like this, than append Dictionary inside that array.

var selectedArray = [[String: AnyObject]]()
let myDictionary: [String: AnyObject] = dataArray[indexPath.row] as! [String : AnyObject]
selectedArray.append(myDictionary)

or if you want the array of string [String] then you need to append that specific String from that dictionary like this.

var selectedArray = [String]()
let myDictionary: [String: AnyObject] = dataArray[indexPath.row] as! [String : AnyObject]
if let str = myDictionary["Key"] as? String {
    selectedArray.append(str)
}
Sign up to request clarification or add additional context in comments.

Comments

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.