0

I have a dictionary with objects that contains array. For example let's sat each object Factory has object manager factory.manager.

Dictionary:

["key1": factory1]
["key2": factory2]
["key3": factory3]

I need to get all managers by transforming dictionary to an array:

[factory1.manager, factory2.manager, factory3.manager]

How can I do it using map for example by avoiding using for cycle.

1
  • Array(dictionary.values.map{$0.manager}) Commented Mar 18, 2016 at 14:13

2 Answers 2

2

This will create an Array of those values:

   Array(dictionary.values.map{$0.manager})

assuming the dictionary instance is of type [String:Factory]

Sign up to request clarification or add additional context in comments.

7 Comments

hey dude, thanks) one more question can I do it for getting Set?
yea it really returns what I mention )
you sound surprised :)
a set can be created: Set(dictionary.values.map{$0.manager})
cannot invoke initializer Set<>
|
-1
class Factory{

    var manager:String = ""

}

var myFactoryDict = ["key1":Factory(),"key2":Factory(),"key3":Factory()]

myFactoryDict.values.map { (factoryObj:Factory) -> String in

    return factoryObj.manager
}

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.