Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • the I print you solution I'm getting "[Optional(1), Optional(2), Optional(10)]". How can I just print the array of numbers ? Commented Feb 25, 2017 at 2:53
  • 1
    let arrayIntegers = stringNumbers.components(separatedBy: " ").flatMap{ Int($0) } Commented Feb 25, 2017 at 2:55
  • 1
    @LeoDabus Thanks. Updated. Forgot just using map will give an array of optionals in this case. And using flatMap leaves out any nil values if the string contains any text that can't be converted to a number. Commented Feb 25, 2017 at 2:57
  • you can also use this for null value let arrayIntegers = stringNumbers.components(separatedBy: " ").flatMap{ Int($0) as? String ?? "" } Commented Feb 25, 2017 at 6:12
  • @seggy There's no need to use ?? with flatMap. And Int will never be a String so it makes no sense at all. Commented Feb 25, 2017 at 7:02