Apple Swift & Xcode for those who care!
Updated..
I started learning Swift version 1 back in the dark ages, 2014-ish. We are now up to version 5 and version 6 is due soon. Apple still has its older language – Objective C but Swift is much easier to learn.
Swift is a modern language with a simpler syntax, more like C# and although I haven’t programmed in Java, it’s all about the syntax and frameworks (libraries) that the language gives developers to play with. How we use those frameworks is up to us. Swift still uses curly braces “{ }“ as the begin and end of some statements but semi colons are gone, unless you want to put several statements on one line. In Swift 3, they removed C style increment and decrement operators, for example i++ which are prone to errors. It is also fast and what more could one hope for in an iPhone app.
Xcode is the interactive development environment (IDE) where you develop Swift (or objective C) apps. What I love about the IDE is that when you create your app and run it, Xcode pops up an iPhone simulator on the Mac. In the simulator, I can see what my app is going to look like before I put it on the phone. You’ll see some screen dumps throughout my blog, these are from the simulator. Xcode is a pleasure to work with and as the language matures, so does the development environment. You can virtually build a quick app with very little code which is great for prototyping.
The database I use is SQLite and is based on SQL (structured query language). Also open source and free to use. It is able to be stored on the device and is very fast as you will see by the Search Sites screen. All ParksnPeaks sites and the QSO Log are stored in a database on your iPhone/iPad. SQL is used throughout the app to insert, update and query the database.
Swift can also be used for developing applications for the Mac, Apple Watch, Apple TV and Linux. Swift is also server side, from low level stuff like networking protocols, security and encryption, web sockets to server side scripting. IBM has embraced it as well and Swift is also available for the Rasberry Pi.
Below is a screenshot of the IDE with a simple test app I created recently. This is the current version of Xcode. Normally I wouldn’t have the code window displaying but thought I’d show it for clarity.
-
On the left is the project view showing all the files included. Depending on the app template used, in this case a single view template, it automatically created the files I needed. Apple does include some placeholder functions & reminders in the code when it is created but it’s otherwise a blank canvas.
-
To the right of the project view is the storyboard (aka Interface Builder), this is where you can design the look of that killer app. I’ve added a text field, a label and a button and I’ve coloured the button blue background with white text. I’ve also coloured the background of the screen light blue. This is all done in the property inspector (last on the right).
-
Right of the Storyboard is the code behind the storyboard. I hooked up the text field, label and button to the code window in the form of @IBOutlets at the top. These outlets make the controls visible to the code.
-
I also hooked the button up to an @IBAction (aka event) at the bottom, which causes the button to do something when pressed. You can see the code I wrote under the @IBAction function:
if edit.text != “” {
label.text = edit.text
}In other words, when the button is pressed, if the text I entered into the edit box is not empty, then show the contents of the edit box in the label.
This code window is normally opened only when hooking up controls on the storyboard to code. The code is kept in ViewController.swift.
-
Right of the code window is the property inspector. I’ve selected the label and you can see many of the properties you can change to do with a label. These are only the common ones, there are many more you can use in code.
Looks a bit messy like this, so here is the finished result.
For more information about Swift see Wikipedia – Swift and Swift.org. Yes its open source and its free but you will need a Mac and Xcode (also free) which comes with Swift.
If you are interested in learning Swift, you can do a free online course through Stanford University.
I also recommend CodeWithChris tutorials, I learnt so much from his lectures. In the screen shot below is a shot of ParksnPeaks on his wall of fame.