Swift Initialize Array

Initialize string arrays and int arrays. Use the Array class and the split func.

Initialize array. In Swift we often create arrays. But many forms of syntax can be used. Some forms are more verbose, and others easier to read.Array

With the Array class, we can specify the element type directly. But this is not always needed. A lightweight syntax is available for arrays.

Initializations. Let us begin with this example. In it we create 4 different arrays. The arrays all have the same elements (saying "the cat is cute").

Example 1: Here we specify the 4 strings in square brackets. We do not specify "String" anywhere.

Example 2: We create an empty Array and then append elements to it. This version requires more lines of code.

Example 3: Uses an "arrayLiteral" argument to the Array initialization method. This requires just one line.

Example 4: Initializes an empty String array. It specifies the type and then uses append.

Swift program that initializes arrays // Initialize array with 4 strings. let words = ["the", "cat", "is", "cute"] print(words) // Initialize with Array class and element type. var words2 = Array<String>() words2.append("the") words2.append("cat") words2.append("is") words2.append("cute") print(words2) // Initialize with an arrayLiteral argument. let words3 = Array<String>(arrayLiteral: "the", "cat", "is", "cute") print(words3) // Initialize as empty array. var words4: [String] = [] words4.append("the") words4.append("cat") words4.append("is") words4.append("cute") print(words4) Output ["the", "cat", "is", "cute"] ["the", "cat", "is", "cute"] ["the", "cat", "is", "cute"] ["the", "cat", "is", "cute"]

Initialize with split. This version of Swift code is less efficient. But we can initialize string arrays from string literals. Sometimes this is required.Split
Swift program that splits string import Foundation // An array can be initialized from a string. // ... This approach is slower. let words5 = "the cat is cute".components(separatedBy: " ") print(words5) Output ["the", "cat", "is", "cute"]

Array initialize error. Sometimes things do not go our way in life. Here we forget to specify an initialization. We specify the array type, but this is not enough.
Initialization error: Swift var codes: [Int] // We need to assign "codes" to an empty array before using append. codes.append(1) Output /Users/.../Desktop/Test/Test/main.swift:2:7: Variable 'codes' passed by reference before being initialized

An initialization fix. Here we fix the array initialization error from the previous example. We just use an empty array, and the code works.
Swift program that initializes empty Int array var codes: [Int] = [] // This code sample works. codes.append(1)

In Swift, array initialization can be done in many ways. Typically, I prefer the shortest syntax form possible. But this does not work when array initializations are complex.
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to