Tables and arrays

Continuing with the iOS8 and Swift tutorials I’m at a level where I can make practical use of some of the knowledge I’ve acquired from these iOS 8 and Swift video tutorials. Lecture 45 provides a task to create an app to display the times table of a number selected from a slider bar. The thing to remember about all programming is good practice to avoid any errors and to be able to interpret errors to create a solution. Sometimes, though it is just the logic that needs to be evaluated if something isn’t behaving as expected.

tableresultBuilding up to this we learn about tables in Swift and how to display items in a table from an array of items. Rob filled his array with members of his family and out put them to each row. I filled my array with the names of my siblings. Unfortunately, when the app ran the display wasn’t what I wanted. The intention was to have each name on each row.

Luckily, this was not too difficult to fix. With a lot of programming languages certain characters are reserved and needs to be escaped if it is intended to be included in a string but not with Swift.

cellcontentMy array declaration had the commas within double quotes. So Swift treated the contents as a single string item for one array element. A simple mistake but something to bear in mind.

array_errorAnother novice mistake involves the error message “Array index out of range” which occurs when you try to refer to an array element which doesn’t exist. So if your array has nine elements (not counting 0!) and you refer to the tenth e.g. array[10] then Swift won’t be happy with it.

Attention to detail is key. The less simple mistakes you make, the more time you save with debugging. - Tak

 

 

Sales and marketing executive.
3rd degree black belt in Taekwon-Do.
Lives at the gym.
Enjoys burgers and cake.
Likes fast cars and lie-ins.

Prime numbers and playgrounds

As coding gets more involved it is very important to get into good practice which will save you a lot of time from finding out what went wrong. I myself spent many hours going through code line by line with some basic school boy errors. In my last blog I wrote about how Swift as a forgiving language. This week I found there were a few things it was not so forgiving about.

In the third app Is it Prime a lot of the back end code is done in Playgrounds and most of my frustrations were from there.

Statement cannot begin with a closure expression

Statement cannot begin with a closure expression

“Statement cannot begin with a closure expression”. What?! When I get a problem I do what most people do. I use Google! But putting this error message into Google wasn’t too helpful, there were many results but nothing very helpful.

The cause? It’s to do with the operand. In C, PHP and Javascript it is perfectly fine to do something like “if number !=2 …” but Swift doesn’t like this! There needs to be a space between a number and the operand for very good reasons I’m sure which I will find out later on. So just not putting in the space was stopping my app from comping and altering to “if number != 2 …” ensured I could continue.

The second most frustrating problem I had was “Prefix/postfix ‘=’ is reserved”, again I thought I could Google the answer, again Google came up short but this problem was quicker to fix.

prefix postfix error

In most programming languages such condition would be valid but in Swift having an exclamation mark at the end of a variable name means you want to unwrap it (telling Xcode the variable definitely has a valid value). So while I had “if number!=2 &&…” it should have been “if number !=2 &&…” all from missing a space! Such simple mistakes which causes such long delays.

In Rob’s iOS and Swift video tutorials he encourages you to pause the video, have a go and come back to see how his solution. So although he may take 7 or 8 minutes in a video lecture and you maybe spending an hour or 2. I can assure you that this is not out of the ordinary. And it is part if the learning process. And remember, if you get stuck the forums are available!

Keep coding! - Tak.

Sales and marketing executive.
3rd degree black belt in Taekwon-Do.
Lives at the gym.
Enjoys burgers and cake.
Likes fast cars and lie-ins.