-2

I have a question, is it possible to append an array of string in an other array. (in Swift)

var array: [String] = []
var array2: [String] = []

array.append("test")
print(array)
example: ["test", "test", "test", "test"]

How can I append just the string "test", "test", "test", "test" in array2 in ()?

print(array2)
example: [("test", "test", "test", "test")]

In the end I would like to have this as result:

print(array2)

[("test", "test", "test", "test"),("test", "test", "test", "test"), ("test", "test", "test", "test")]
  • @shadowof you should add answers, if you're going to write the answer anyway :) – SpacyRicochet Jun 03 '16 at 12:59
  • 2
    Possible duplicate of [Add an element to an array in Swift](http://stackoverflow.com/questions/24002733/add-an-element-to-an-array-in-swift) – Sohil R. Memon Jun 03 '16 at 13:01
  • 1
    @SpacyRicochet question too stupid to perform more than 15 symbols and make answer; also its probably duplicate or even illegal (syntax) – Yury Jun 03 '16 at 13:02
  • 1
    The information you need is here: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html#//apple_ref/doc/uid/TP40014097-CH8-ID105 – Eric Aya Jun 03 '16 at 13:09

3 Answers3

1

Do you mean:

let array: [String] = ["test1", "test2", "test3"]
var array2: [String] = ["test4", "test5", "test6"]

array2.appendContentsOf(array)

print(array2)
Chris
  • 2,739
  • 4
  • 29
  • 57
1

Instead of appendContentsOf(_:), you can use the += operator to append to an existing array:

var a = [1, 2, 3]
let b = [4, 5, 6]

a += b

print(a)
Alexander
  • 59,041
  • 12
  • 98
  • 151
0

The best way to do this would be as follows:

let array1   = ["Something"]
let array2   = ["Else"]
let combined = array1 + array2
print(combined) // ["Something", "Else"]

or

let array1 = ["Something"]
var array2 = ["Else"]
array2 = array1 + array2
print(array2) //["Something", "Else"]
SpacyRicochet
  • 2,269
  • 2
  • 24
  • 39