0

I'm currently doing this to get an array of strings with users phone numbers :

 if let phoneNumbers = contacts.map({$0.phoneNumber}) as? [String] { }

Contact Object:

class Contact {

    var name: String!
    var phoneNumber: String!
}

How can instead of getting an array of string with the phone numbers get an array of strings with each of the contacts name and phone number? I want to include both values in the string array instead of just the phone number.

Thanks

Edit:

Current output is something like this ["3453534534", "34534535345"] where those are phone numbers.

The output that I hope to achieve is something like this ["john" : "12312312", "robert" : "32423423423"]

6
  • What exactly do you mean? Could you explain it better, and also give us some sample input and output? Commented Dec 17, 2016 at 21:24
  • stackoverflow.com/help/mcve Commented Dec 17, 2016 at 21:24
  • @AlexanderMomchliov I'm sorry Alex. I just added some sample output and desired output Commented Dec 17, 2016 at 21:26
  • Are you aware that output is not an Array? Commented Dec 17, 2016 at 21:26
  • @AlexanderMomchliov Got it. Thanks for the heads up Commented Dec 17, 2016 at 21:27

1 Answer 1

1
var dulce = [String: String]()
contacts.forEach {
    dulce[$0.name] = $0.phoneNumber
}

This did the job in the least amount of code possible!

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.