Software Development

Pro tip: Create and use arrays in Apple's Swift programming language

Arrays have taken on a new form in Apple's Swift compared to Objective-C. We show you what's changed, and how to use these data structures in Swift.

appleswiftarrayshero072814.png

Arrays are the building blocks for modern programming languages, allowing fast memory access to primitive data types, language objects, and custom objects. Apple's Swift beefs up this data structure's ease of use when doing things like using ranges to find objects between certain indices, overriding the + operator to array joins and add objects, and much more. We'll show you how to create and use arrays in Swift.

Allocating arrays

To allocate an empty array, use this syntax:

<code>var myArray = Int[]() 

Note that arrays in Swift are typed, and when creating an array you must specify the data types that will be inserted into the array. Type will be inferred if you allocate an array and initialize it with objects right out of the gate like we do below; otherwise, it must be explicitly specified.

To allocate an array with objects inside, use this syntax:

<code>var ids = [1, 2, 3, 4] 

You can still use the static typing when allocating and initializing your arrays, like this:

<code>var ids: Int[] = [1, 2, 3, 4] 

Adding objects to arrays

Adding objects to arrays couldn't be easier. You can use the overloaded + operator to easily add a new object, like this:

<code>var myArray = ["Hello"]  var myString: String = "World" myArray += myString 

Once you use the overridden + operator, the myString object will be automagically written to the array (Figure A), and the array will now have "World" as the second object.

Figure A

appleswiftarraysfiga072814.png

Changing objects in arrays

To have an object in the array, you can use the convenience lookup operator to access a particular object, or to replace the object in the array. Look at the following scenario:

<code>var myArray = ["Hello", "World"] 

If you want to replace the second object in that array (the "World" string) with "TechRepublic," you can do the following:

<code>myArray[1] = "TechRepublic" 

In the brackets, place the array index that you'd like to work with. Now if you look at the array, it will be updated.

Remember: The array indexing starts with a 0 index and counts up, so the second object is at index 1 and not 2.

Array subset

If you wish to get a subset of the items in an array, you can do that using the new range features. You can always get a single object in an array by specifying the index, like this:

<code>var myObject = myArray[2] 

For more complex object manipulations, you may need to get a particular range of objects and place it into an entirely new array that contains a subset, like this:

<code>var myArray = ["One", "Two", "Three", "Four", "Five"]  var subsetArray = myArray[0..2] 

Using the two dots between the two numbers representing the range means to start at index 0 and include all objects in indexes through index 2, but not including 2.

If you wish to include the object represented by the last number in the range, you'll want to do this instead:

<code>var myArray = ["One", "Two", "Three", "Four", "Five"]  var subsetArray = myArray[0...2] 

This subtle difference of using three dots instead of two means that you want to start at 0 and go up to the final index and include that final object at that index. The output of this second example (Figure B) would be:

<code>["One", "Two", "Three"] 

Figure B

appleswiftarraysfigb072814.png

Joining arrays

Joining arrays, like adding objects to the array, couldn't be easier. Again, you'll use the overloaded + operator to join two different arrays.

Let's take a look at this example:

<code>var myFirstArray = ["Hello"] var mySecondArray = ["World"]  myFirstArray += mySecondArray 

After doing this, the first array will handle the join of adding the objects from the second array into it (Figure C).

Figure C

appleswiftarraysfigc072814.png

Constant arrays

If you define an array as a constant like in the following example, because this array is a constant you cannot change the size of the array by adding or removing items.

<code>let items = ["Apple", "Orange", "Pear"] 

You can modify the contents of a particular index in the array if it does, in fact, exist. For example, this will change the second item ("Orange") to "Banana" (Figure D).

<code>items[1] = "Banana" 

Figure D

appleswiftarraysfigd072814.png

Summary

Arrays are fast, easy to implement, and even better to use in Swift, thanks to the convinces that Swift affords developers. The Swift documentation for arrays is a great resource for learning even more about this refreshed data structure in Swift.

Note: Swift is still in beta and is being tweaked by Apple, so you should always refer to the Swift books and documentation for the most up-to-date resources in case the handling of code changes in the future.

Have a tip or trick for using Swift's array data structure? Let us and other developers know in the discussion.

Also see

Disclaimer: TechRepublic and ZDNet are CBS Interactive properties.

About

Cory Bohon is an indie developer specializing in iOS and OS X development. He runs a software company called Cocoa App and is also a developer at MartianCraft. He was introduced to technology at an early age and has been writing about his favorite te...

1 comments

Editor's Picks