2

I have a dictionary with multiple dictionary data :

{
 1455201094707 = {
 };
 1455201116404 = {
 }: 
 1455201287530 = {
 };
}

I have to add all these dictionaries to an array in swift. How to iterate dictionary as :

for let tempDict in dataDictionary
{
 self.tempArray.addObject(tempDict)
}

Error "let pattern cannot appear nested in an already immutable context"

for tempDict in dataDictionary as! NSMutableDictionary
{
 self.tempArray.addObject(tempDict)
}

Error : Argument type element(aka(key:AnyObject, value:AnyObject)) argument type does not conform to expected type anyobject

for tempDict in dataDictionary as! NSMutableDictionary
{
 self.tempArray.addObject(tempDict as! AnyObject)
}

Error: Could not cast value of type '(Swift.AnyObject, Swift.AnyObject)' (0x1209dee18) to 'Swift.AnyObject' (0x11d57d018).

for tempDict in dataDictionary
{
 self.tempArray.addObject(tempDict)
}

Error: Value of Type AnyObject has no member generator

Edit

I want the final array as :

(
 {
 1455201094707 = {
 };
 }
 {
 1455201116404 = {
 }:
 } 
) 

What is the correct way to implement this?

Any help will be appreciated.....

I have used code :

var tempArray:[NSDictionary] = []
 for (key, value) in tempDict {
 tempArray.append([key : value])
 }

Error: value of type AnyObject does not conform to expected dictionary key type NSCopying

code :

let tempArray = tempDict.map({ [0ドル.0 : 0ドル.1] }) 

Error : type of expression is ambiguous without more context

asked Feb 16, 2016 at 11:47

2 Answers 2

5

First of all, when you use

for let tempDict in dataDictionary {
 self.tempArray.addObject(tempDict)
}

Swift gives you tuple like (key, value) in tempDict.

So you should iterate like this

for (key, value) in sourceDict {
 tempArray.append(value)
}

Note: I used here native swift structures, and my advise - to use them as often as possible (instead of ObjC ones)

Or you can use map-function on dictionary.

let array = sourceDict.map({ 0ドル.1 })

Edit. For

(
 {
 1455201094707 = {
 };
 }
 {
 1455201116404 = {
 }:
 } 
) 

use

for (key, value) in sourceDict {
 tempArray.append([key : value])
}

or

let array = dict.map({ [0ドル.0 : 0ドル.1] })

Note. if you use NSDictionary you should cast it to swift Dictionary

if let dict = dict as? [String: AnyObject] {
 let array = dict.map({ [0ドル.0 : 0ドル.1] })
 print(array)
}
answered Feb 16, 2016 at 12:22
8
  • Thank you for your answer. I have edited the question, can you tell me how to do that..?? Commented Feb 16, 2016 at 12:51
  • I have used code : var tempArray:[NSDictionary] = [] for (key, value) in tempDict { tempArray.append([key : value]) } Error: value of type anyobject does not conform to expected dictionary key type nscopying Commented Feb 16, 2016 at 13:23
  • code : let array = dict.map({ [0ドル.0 : 0ドル.1] }) gives error : type of expression is ambiguous without more context Commented Feb 16, 2016 at 13:30
  • Again, I used native swift types. You should better use them everywhere or cast to them. Commented Feb 16, 2016 at 13:46
  • Thanks @Anton, It is showing an issue " Cast from NSMutableDictionary to unrelated type [string:anyobject] always fails" but output is correct. One more thing , how to add this array to NSmutableArray , to show data in tableView ? Commented Feb 16, 2016 at 14:14
-2
  1. Get all keys from the dictionary, for this NSDictionary has property allKeys.

  2. Loop all keys, and add object for the key to the array.

  3. Before the all steps above, read documentation!

Good luck!

answered Feb 16, 2016 at 11:52
1
  • Thank you for your answer. I have edited the question, can you tell me how to do that..?? Commented Feb 16, 2016 at 12:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.