Swift Tutorial: String and Collection – Part 2

Welcome to the second edition of the String and Collection tutorial! Today, we will learn how to work with collection types in Swift. If you are an Objective-C programmer, you would know that there were several types of collections present in a traditional Objective-C: NSArray NSMutableArray NSDictionary NSMutableDictionary NSSet NSMutableSet However, there are basically just two types of collections available in Swift. Array Dictionary Please note that, in Swift, the array and the dictionary contains objects of strongly typed. This means that if you want to store string data, we will not go on to add another type where NSArray […]

Welcome to the second edition of the String and Collection tutorial! Today, we will learn how to work with collection types in Swift. If you are an Objective-C programmer, you would know that there were several types of collections present in a traditional Objective-C:

  • NSArray
  • NSMutableArray
  • NSDictionary
  • NSMutableDictionary
  • NSSet
  • NSMutableSet

However, there are basically just two types of collections available in Swift.

  1. Array
  2. Dictionary

Please note that, in Swift, the array and the dictionary contains objects of strongly typed. This means that if you want to store string data, we will not go on to add another type where NSArray and NSMutableArray did support. I personally like this approach since it inevitably prevents a lot of runtime error, especially if you’re working with a complex app. Lets look into the new syntax of array and dictionary in Swift:

var names:array<string> = ["sample value 1", "sample value 2", "sample value 3"]
Notice that we are assigning the values in initializing. We can see that Swift is smart enough since we are NOT having to explicitly declare the array type, so we can rewrite as:
var names = ["sample value 1", "sample value 2", "sample value 3"]
As already mention, this is strongly typed. You must not add any object other than string in it. It is also mutable, which means that you can add the object (string, in this case) as a last element in this manner:
names.append(“sample value 4″)
Alternatively you can use:
names+=”sample value 5″
It will also add the value at the end of this array. However, if you want to insert the object in a certain index, then we can use the insert method of array:
names.insert(“New value”,atIndex:0)
How to retrieve a value?
var aValue = names[0];
It will read a value from 0 index from names and store it in “aValue”. Let us explore another new feature of Swift that is subscript. Using this feature, you can now add/replace a range of values: names[1…5] = [“A value”, “B value”]; This basically means that values if array from 1 to 5 is simply being replaced by the 2 values- “A value” & “B value”.

Iterating Over an Array

You can iterate over the entire set of values in an array with the for-in loop:

for name in names{ println(name); } 
There is also another interesting feature where we can iterate both with index and as well as values in Swift Array:
for (index,name) in  enumerate( names){ println("you are accessing : (name) at position: (index)" }

Creating and Initializing an Array

In Swift, even without setting any initial values, you can choose to create an empty array of a certain type using the initializer syntax:

var someInts = Int[]() println("someInts is of type Int[] with (someInts.count) items.") // prints "someInts is of type Int[] with 0 items."
Please note that the type of the someInts variable is inferred to be Int[], because it is set to the output of an Int[]initializer. Alternatively, if the context already provides the type information such as a function argument or an already-typed variable or constant, you can create an empty array with an empty array literal, which is written as [](an empty pair of square brackets):
 someInts.append(3) // someInts now contains 1 value of type Int someInts = [] // someInts is now an empty array, but is still of type Int[]
Swift’s Array type also provides an initializer for creating an array of a certain size with all of its values set to a provided default value. You pass this initializer the number of items to be added to the new array (called count) and a default value of the appropriate type (called repeatedValue):
var threeDoubles = Double[](count: 3, repeatedValue: 0.0) / threeDoubles is of type Double[], and equals [0.0, 0.0, 0.0]
Thanks to this type inference, you don’t need to specify the type to be stored in the array when using this initializer since it can be inferred from the default value:
var anotherThreeDoubles = Array(count: 3, repeatedValue: 2.5) // anotherThreeDoubles is inferred as Double[], and equals [2.5, 2.5, 2.5]
Our final lesson for the tutorial involves the creation of a new array by adding together two existing arrays of compatible type with the addition operator (+). The new array’s type is inferred from the type of the two arrays you just added together:
var sixDoubles = threeDoubles + anotherThreeDoubles // sixDoubles is inferred as Double[], and equals [0.0, 0.0, 0.0, 2.5, 2.5, 2.5]
If you want to know more about this, please do visit the Apple Developer website about Swift. In our next Swift tutorial, I will walk you guys through dictionary. You can check part one of this tutorial here.

Md. Arifuzzaman Md. Arifuzzaman is a Senior Software Engineer at SELISE rockin’ software. He has extensive experience in diverse facets of C#, .NET, BI development, and mobile app development (Objective-C, Android).

Leave a Reply

Your email address will not be published. Required fields are marked *

+1
Share
Share
Tweet