Constants; Conventions and Rules; Strings

With renewed vigour, I'm restarting my Swift journey, going over familiar ground! This has happened before, as it's comforting and easy to go through information I already know. However it is necessary and important. Along with the Treehouse tutorial videos, I've found another eBook 'App Development with Swift', which I'll use to supplement and check any of the key points. 

Swift Basics Video 4: Constants

The analogy he makes here is that computers are not smart enough to figure out if something has changed. The purpose of constants is to create a value that will NEVER be changed. We can see what is inside the box (the glass box analogy!) but CANNOT change it!

let language = "Swift"

This is a simple example to show how a constant is created. If we try to change the value of 'language', then there will be errors! This is because it is a constant. Makes total sense. Xcode will show the 'red error' with the icon, which will then give a suggestion for how to fix the problem.

A variable containing a piece of data is known as a MUTABLE type. A constant is of IMMUTABLE type as the value CANNOT change! 'Let' stems from mathematical usage...not quite sure why!

On Xcode, the bottom bar is called the 'console'. This is different to normal code as a REPL (read, evaluate, print, loop) - which is what Playgrounds are for!

This is the challenge after the tutorial video:

Create a constant named favoriteDessert and assign to it a string containing your favorite dessert. Remember the syntax for a string is some text enclosed with two double quotes.
Pro Tip: Dessert is spelled with two s'

This should be easy enough!

let favoriteDessert = "Key Lime Pie"

Swift Basics Video 5: Conventions and Rules

This was all pretty familiar - the sidebars, results, print, Camel Case (camelCase) etc. Nothing much to write here but useful to recap!

Swift Basics Video 6: Strings

There is a bit more about the navigator - the left hand bar and selecting the file. Now we're moving on to creating a constant:

let country = "United Arab Emirates"

All straightforward - the syntax of the keyword of let, the name of the constant and the string being written within speech marks. 

Concatenation - adding two strings together. I've done this as an example of country, city and area. However, the new string does not have spaces. So this is where String Interpolation comes in, or modifying the string itself.  The backslash is part of the syntax needed to use interpolation.

let emirate = "Dubai"

let area = "JVC"

let address = country + emirate + area

let newAddress = "\(country), \(emirate), \(area)"

I've moved ahead to the best solution, rather than adding in spaces/commas within the speech marks. 


Otherwise, without String Interpolation and using Concatenation, it would look like this:

let newAddress2 = country + ", " + emirate + ", " + area

Same result of "United Arab Emirates, Dubai, JVC" but the String Interpolation method is much more elegant! 

So all of this is reassuring and familiar. The real challenge will be when I've finished going over all of the course videos leading back to where I got to (enumerations), and then see how I can take it forward. I am enjoying going back to basics as it gives me confidence that I DO know a lot, rather than blindly going through an eBook that makes me feel a bit useless! There's nothing to read alongside the information in the past three videos, as it's all logical and no questions have come up. I will use the Swift eBook and any other information to help understand concepts that aren't as clear, as they come up. My target of an hour per week has already been surpassed! This week, it will be more so, as it is not NEW information. I reckon by the end of next week, I will have finished my 'consolidation course. Another entry soon!

Comments

Popular posts from this blog

*Xcode Project Entry 2* F1 Quiz - part 1

Angela Yu Course Part 10 (up to lesson 112)

Angela Yu Xcode 12 Course - Part 7 (lectures 74 to 79)