Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an empty array in Swift?

Tags:

arrays

ios

swift

People also ask

How do you make an array empty in Swift?

To create an empty string array in Swift, specify the element type String for the array and assign an empty array to the String array variable.

How do you declare an array in Swift?

Array in swift is written as **Array < Element > **, where Element is the type of values the array is allowed to store. The type of the emptyArray variable is inferred to be [String] from the type of the initializer. The groceryList variable is declared as “an array of string values”, written as [String].

How do I check if an array is empty in Swift?

To check if an array is empty, use Swift function isEmpty on the array. Following is a quick example, to check if an array is empty. The function returns a boolean value. If the array is empty, isEmpty returns true, else false.


Here you go:

var yourArray = [String]()

The above also works for other types and not just strings. It's just an example.

Adding Values to It

I presume you'll eventually want to add a value to it!

yourArray.append("String Value")

Or

let someString = "You can also pass a string variable, like this!"
yourArray.append(someString)

Add by Inserting

Once you have a few values, you can insert new values instead of appending. For example, if you wanted to insert new objects at the beginning of the array (instead of appending them to the end):

yourArray.insert("Hey, I'm first!", atIndex: 0)

Or you can use variables to make your insert more flexible:

let lineCutter = "I'm going to be first soon."
let positionToInsertAt = 0
yourArray.insert(lineCutter, atIndex: positionToInsertAt)

You May Eventually Want to Remove Some Stuff

var yourOtherArray = ["MonkeysRule", "RemoveMe", "SwiftRules"]
yourOtherArray.remove(at: 1)

The above works great when you know where in the array the value is (that is, when you know its index value). As the index values begin at 0, the second entry will be at index 1.

Removing Values Without Knowing the Index

But what if you don't? What if yourOtherArray has hundreds of values and all you know is you want to remove the one equal to "RemoveMe"?

if let indexValue = yourOtherArray.index(of: "RemoveMe") {
    yourOtherArray.remove(at: indexValue)
}

This should get you started!


Swift 5

There are three (3) ways to create a empty array in Swift and shorthand syntax way is always preferred.

Method 1: Shorthand Syntax

var arr = [Int]()

Method 2: Array Initializer

var arr = Array<Int>()

Method 3: Array with an Array Literal

var arr:[Int] = []

Method 4: Credit goes to @BallpointBen

var arr:Array<Int> = []

var myArr1 = [AnyObject]()

can store any object

var myArr2 = [String]()

can store only string


You could use

var firstNames: [String] = []

There are 2 major ways to create/intialize an array in swift.

var myArray = [Double]()

This would create an array of Doubles.

var myDoubles = [Double](count: 5, repeatedValue: 2.0)

This would create an array of 5 doubles, all initialized with the value of 2.0.


If you want to declare an empty array of string type you can do that in 5 different way:-

var myArray: Array<String> = Array()
var myArray = [String]()
var myArray: [String] = []
var myArray = Array<String>()
var myArray:Array<String> = []

Array of any type :-

    var myArray: Array<AnyObject> = Array()
    var myArray = [AnyObject]()
    var myArray: [AnyObject] = []
    var myArray = Array<AnyObject>()
    var myArray:Array<AnyObject> = []

Array of Integer type :-

    var myArray: Array<Int> = Array()
    var myArray = [Int]()
    var myArray: [Int] = []
    var myArray = Array<Int>()
    var myArray:Array<Int> = []