1

My question:

This answer explains how to convert a String containing elements separated by spaces into an array.

let numbers = "1 2 3 4"
let numbersArray = numbers.components(separatedBy: " ")
print(numbersArray)
// output: ["1", "2", "3", "4"]
// but I want: [1, 2, 3, 4]

However, I'm trying to make an array without quotation marks, because I'm making an array of numbers, not strings.

My attempts:

I tried removing all quotation marks from numbersArray, but this didn't work as it's an array, not a string.

numbersArray.replacingOccurrences(of: "\"", with: "") // won't work

I tried something different: I tried adding each element in the array to a new array, hoping that new array wouldn't contain quotation marks. I got an error, though:

let numbers = "1 2 3 4" // string to be converted into array without quotes
let numbersArray = numbers.components(separatedBy: " ") // convert string into array with quotes
var newNumbersArray = [String]() // new blank array (which will be without quotes)
for i in numbersArray { // for each item in the array with quotes
  newNumbersArray += i // (hopefully) add the item in the new array without quotes
} 
print(newNumbersArray) // print the new array 

This gives me an error:

Swift:: Error: cannot convert value of type '[String]' to expected argument type 'inout String'
  newNumbersArray += i

3 Answers 3

2

You can apply a flatMap call on the [String] array resulting from the call to components(separatedBy:), applying the failable init(_:radix:) of Int in the body of the transform closure of the flatMap invokation:

let strNumbers = "1 2 3 4"
let numbersArray = strNumbers
    .components(separatedBy: " ")
    .flatMap { Int($0) }

print(numbersArray) // [1, 2, 3, 4]
print(type(of: numbersArray)) // Array<Int>
Sign up to request clarification or add additional context in comments.

1 Comment

Actually this answer is pretty sweet
1

You can try this:

var newArray = [Int]()

for item in numbersArray{
    newArray.append(Int(item))
}

print(newArray)

5 Comments

This worked! I did need to add an ! after Int(item) to unwrap it though
@SkeletonBow I encourage you to try this approach but appending ! for say let numbersArray = "1 2 a 3 4". As a rule of thumb, if you're not entirely familiar with optionals, your should never use the forced unwrapping operator !.
Okay, thank you. My app does crash. I just wanted to use ! here because it's much simpler and allows for more readable code, but I will keep your suggestion in mind and try a better approach (using if let or something)
@SkeletonBow or possible the safe and quite readable flatMap approach shown in my answer below (which also allows you to let the newArray be immutable).
I'm still a beginner, so I didn't quite understand your answer. I'll make a note to look at yours when I'm more experienced, though. In that way I'll actually be able to understand what I'm doing better :)
1

Swift 3.0

Try this.. Chaining method makes it easy.

let temp = "1 2 3 4 5 6"
var numbers: [Int] = []

temp.components(separatedBy: " ").forEach { numbers.append(Int($0)!) }

print(numbers) //[1, 2, 3, 4, 5, 6]

1 Comment

I would have selected this as the answer, but the current accepted answer is basically what I was trying to do, but corrected. This is also a very good answer, though.

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.