Search code examples
arraysswiftnsarray

Is there a Swift Array equivalent for NSArray.arrayByAddingObjectsFromArray()?


I would like to gain a new array by appending elements to an existing array without modifying it. NSArray has arrayByAddingObjectsFromArray(), e.g.:

let numbers = (["one", "two", "three"] as NSArray).arrayByAddingObjectsFromArray(["four", "five"]) as! [String]

Is there an equivalent for Swift's Array struct that would make bridging to NSArray unnecessary?


Solution

  • Do you looking for this one? Your existing array does not modify.

    let existingArray = ["four", "five"]
    let numbers = ["one", "two", "three"] + existingArray