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!

2
  • 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. Commented Jun 15, 2014 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? Commented Jun 15, 2014 at 14:16

3 Answers 3

15

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]

Sign up to request clarification or add additional context in comments.

4 Comments

Whats the difference between "+=" and "append"?
@frankzk it's the same.
@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.
(2/2) This extra type checking takes time. See stackoverflow.com/questions/29707622/… and stackoverflow.com/a/48916182/5175709 @Suisse
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.

Comments

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"]

Comments

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.