This is a good application for map. Use it to take each item in your array and apply trimmingCharacters(in:) to remove the whitespace from the ends of your strings.
import Foundation // or UIKit or Cocoa
let array = ["Apple ", "Bike ", " Cat", " Dog "]
let trimmed = array.map { $0.trimmingCharacters(in: .whitespaces) }
print(trimmed) // ["Apple", "Bike", "Cat", "Dog"]
@DuncanC's comment is an important point so I'm highlighting it here:
@vacawama's use of the map statement is certainly more "Swift-like" than using a
for loop, but I bet the performance is all but identical. There is no
magic here. If you need to check all the strings in an array and trim
leading/trailing spaces then you need to iterate through all the
strings. That is what the map statement does internally. It just uses
functional rather than imperative syntax.
nilwhen you're accessing this array. I'm I right?