1

I use method .keys from the Dictionary to fetch all keys that are in the dictionary and work with them like Array.

Problem when i fetch it .keys it returns LazyMapCollection how i can convert it to Array of keys.

Precondition:

User is structure with funds not nil dictionary [String:String].

let keys = user.funds?.keys

How should i convert keys to be Array?

0

1 Answer 1

9

Maybe using map will help:

let dictionary: [String:String] = [:]
let keys: [String] = dictionary.map({ $0.key })

Don't like map? Go this way:

let dictionary: [String:String] = [:]
let keys: Array<String> = Array<String>(dictionary.keys)
Sign up to request clarification or add additional context in comments.

4 Comments

I know this but how i can do this with help of .keys
@OlegGordiichuk updated the answer
Thx for the response
Great answer. I wanted something other then a for loop and I used the 2nd way (no map). I like it better then all the answers in the link to “this question already has an answer here”

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.