Swift Tutorial: String and Collection – Part 1

In order to jump into more complex topics we should first master the basics. Thus, today I will walk through sting and collection. In Swift, we have to tell the compiler the type of data we are dealing with. If we merely assign a value while creating a type, the Swift compiler is strong enough to understand it. Thus, there is no need to explicitly declare a type for each and every case. However, if we only need a type, with a view to assign it later as the project matures, then it becomes important to explicitly declare the type. […]

In order to jump into more complex topics we should first master the basics. Thus, today I will walk through sting and collection.

In Swift, we have to tell the compiler the type of data we are dealing with. If we merely assign a value while creating a type, the Swift compiler is strong enough to understand it. Thus, there is no need to explicitly declare a type for each and every case.



However, if we only need a type, with a view to assign it later as the project matures, then it becomes important to explicitly declare the type. You may find some similarities with this and the var key word in C#.

Let’s take a look at a simple example:

Case 1:

var myfirstStringType = “Hello World”;

Case 2:

var myFirstString:String; myFirstString = “Hello World”;

Case 3 (Compiler error)

var myFirstString ; myFirstString = “Hello World”

As you can see, the first two cases here are quite appropriate. For case 1, despite not explicitly declaring the string type here, the compiler is doing just fine with the string value that we have assigned. In simpler words, the Swift compiler is smart enough to know that it is a string type!

Now for case 2. Here, we are declaring a String type explicitly since, for example, we do not want to assign anything while declaring. Later on, we can come back and assign the String values here.

Case 3 is wrong, and we can see that the compiler is complaining. The first line that we have typed makes it unclear to the compiler about the type of “myFirstString”. Thus, the compiler is unable to allocate proper memory (heap/stack).

The three cases we explored just now can be true for any type. So we must explicitly declare a type or assign a value while declaring. This should make things a bit clear now.

With that out of the way, let us take a look at another very interesting feature- the “Value Type”. For an Objective-C or a C# developer like me, I had hard time believing this. I don’t quite understand why the Swift architects would design it like this, but the concept is somewhat like this. Say, you have a string,

myFirstString = “Hello World”

Now you decide to pass this to a method or reassign it to another variable. In that case, it will always copy the value to the new one. For reference type, it does not only copy the value but also refers the address. Hence, an important takeaway is that the larger the size of the string is, the more the memory becomes an issue. However, the swift developer reference has the following to say:

“Swift’s copy-by-default string behavior ensures that when a function or method passes you a string value, it is clear that you own that exact string value, regardless of where it came from. You can be confident that the string you are passed will not be modified unless you modify it yourself. Behind the scenes, Swift’s compiler optimizes string usage so that actual copying takes place only when absolutely necessary. This means you always get great performance when working with strings as value types.”

Note for Objective-C or C# Developers

In Objective-C, we have NSString and NSMutableString. NSMutableString is mutable but NSString is not.

For C#, we have StringBuilder which is mutable. An interesting thing to note is that Swift, does not come with any such concept; we only have a string class. In this case, if we choose to make it mutable, simply use +.

Here’s an example:

var myfirstStringType = “Hello World”; myFirstStringType += ” Another word with first “; //output: Hello World Another word with first

Swift string is a combination of characters where both use a double quotation.

var aChracter:Chracter = “A”; //only one character is permitted here. Anything greater than one causes compiler error

As string is a combination of characters, the following statement is valid in “Swift” context:

var myfirstStringType = "Hello World"; myfirstStringType += " Another word with first ";

for value in

myfirstStringType{ println(value); }

We can easily add two characters and the result will be a string, and not a character:

var firstCharacter:Character = "A"; var secondCharacter:Character = "B"; let addBothTwo:String= firstCharacter + secondCharacter;

We can also add a string with character:

var firstCharacter:Character = "A"; var secondCharacter:Character = "B"; let addBothTwo:String = firstCharacter + secondCharacter; //result "AB" let nowString = " It is string"; let result = addBothTwo + nowString; //result "AB It is string"

I have taken on the liberty to mention this aspect in detail since it is, in particular, a totally new concept when dealing with characters and sting. The very first time I saw this, I could not help but ask myself why I had to type ‘let’ instead of ‘var’. This, I found out, is because if you want a constant variable, “Swift” gives ‘let’, so that the variable that starts with a ‘let’ shall not be subject to further modification.

String Interpolation (String formatting in C# context)

How to format a string is very easy:

let aValue = 5; let aFormatString = “Your value is (aValue)”; //output: Your value is 5 “()” is used to deal with the value come from outside.

String comparison

Now let’s talk about string comparison. Basically, there are 2 types of comparison. One is where we compare two values (value type), and the other is where we compare to an object (reference type). The syntax, when comparing, is quite interesting.

Please note that a “==” is used to compare values and a “===” is used to compare to reference type objects. So, if you write “if astring == bstring” it is correct and will be executed accordingly.

Also, you will find that Swift comes with a lot of string manipulation functions. I skipped that part since it is widely available. My intention behind this article was to highlight some key features that we are not familiar with in the context of “C# or Objective-C”.

In the second part of the Swift Tutorial, I shall describe with Collection in Swift.