5

I cannot figure out how to add values to an empty array in Swift. I have tried started with empty array in two different ways:

var emptyArray : Int[]?
emptyArray = Int[]()

and

var emptyArray = []

(by the way, what is the difference with these two ways of creating empty arrays?)

I have tried to add an integer to the array with emptyArray.append(1), emptyArray += [1] but none works nor it is in the guide book (or maybe, it is hidden some where that I couldn't figure out). Both of these work if there is one or more values in it and this is driving me crazy! Please let me know how to if you know how to do it. Thank you!

Fried Rice
  • 3,295
  • 3
  • 18
  • 27
  • The difference: #1 creates a generic array that will contain Int's and only Int's. #2 creates a non-generic array that can contain items of any class. – Jean Le Moignan Jun 15 '14 at 12:47
  • Thank you, can you please tell me why when I create an empty array using #1, neither `emptyArray.append(1)` nor `emptyArray += [1]` work? – Fried Rice Jun 15 '14 at 14:16

3 Answers3

14

First, create empty Int array:

var emptyArray : Int[] = []

or:

var emptyArray = Int[]()

Add number to that array (two ways):

emptyArray += [1]
emptyArray.append(2)

Array now contains [1, 2]

juniperi
  • 3,723
  • 1
  • 23
  • 30
  • Whats the difference between "+=" and "append"? – frankzk Jun 10 '15 at 05:38
  • @frankzk it's the same. – Suisse Nov 29 '15 at 20:12
  • @frankzk (1/2) Functionality wise they are identical. But I don't believe they are 100% the same. I believe `append` is better/faster. Why? Because the `+` operator is overloaded. Meaning every time the compiler sees `+` it has to go through series of checks. Like first **identify** the left had side's type. Is it an Int or a String or Array or whatever? Then let's just make sure the right hand side also has the same type. – mfaani Sep 18 '18 at 10:50
  • (2/2) This extra type checking takes time. See https://stackoverflow.com/questions/29707622/swift-compiler-error-expression-too-complex-on-a-string-concatenation/39514629#39514629 and https://stackoverflow.com/a/48916182/5175709 @Suisse – mfaani Sep 18 '18 at 10:53
6

You need to first declare the empty array in Swift like this:

 var emptyArray = [Int]()

You can then append that array with whatever value/variable you so choose like this:

emptyArray.append(6)

just be sure you keep in mind that trying to append a type that mismatches your array declaration will give you a compile error. For example, trying to append a string would error since this array was declared using the Int type.

Playgrounds in XCode are an excellent resource for testing things like this.

Unconquered82
  • 523
  • 1
  • 8
  • 17
0

Swift

     var arrName = [String]()
     arrName = ["Deep", "Hemant", "Yash"]
     print("old array--> ", arrName)
     arrName.append("Jay")
     print("new array--> ", arrName)

Output:- old array--> ["Deep", "Hemant", "Yash"]

new array--> ["Deep", "Hemant", "Yash", "Jay"]

Deepak Tagadiya
  • 2,187
  • 15
  • 28