I’m going to go a bit old school on this one. We’re staying on Swift here, don’t worry, but I want to cover something that has been around since the early days of C. They were probably earlier, but that’s the oldest language besides BASIC that I personally have experience with. We’re going to talk about some classic control flow and how Swift has updated them… or kept them the same, we’ll see.
[Read more…]
Archives for July 2014
Functions in Swift: Parameters and Return Types
I think we’ve gone over the basics of variables in Swift enough. It is time to talk about functions in Swift. Now, we have talked about these to some extent before, and I took it as a given that people would understand a bit. But now I think it is time to actually start diving in and explaining more specifics.
Functions are blocks of code that perform a task. You can then call these functions at different places, which makes fixing bugs much easier than the alternative. If you just copied and pasted the code everywhere, and you found a bug, you would have to change each one to fix that bug. With functions, you just change it one place, and everything that calls it gets the new fixed version of that block of code. The entire app itself is wrapped within a function called “main”, but you don’t normally interact with it, unless there’s a crash. Functions usually take arguments, and very often return data back to where it was called from.
[Read more…]
PregTracker 1.0.1 Released!
I did not really mention it much online, but my wife became pregnant in the fall of 2013. Every time somebody would ask me “how far along is she?” I would have to stop and think, do some mental math, and eventually tell them the answer. I figured there had to be a better way.
Access Control in Swift
The access control in Swift is much more like the access control I am accustomed to in other languages like C# or Java, and I am quite thankful for that. I’m not saying there is anything wrong with Objective-C’s way of doing it, while it was different, I did like having the private properties in the interface block of the implementation (.m) file, and the public properties and methods in the header (.h) file. It made it easy to know exactly where to look.
Nonetheless, access control is here in Swift, so here we go. You can set access control to many things in Swift, particularly properties and methods, but you can even set it to types, initializers, subscripts, or protocols. Swift currently has three levels of access control:
- Public
- Internal
- Fileprivate
- Private
Optional Chaining and Implicitly Unwrapped Optionals in Swift
As promised last time, here is part two of my post about Swift optionals. Here we will discuss a few more advanced aspects on how to deal with optionals, optional chaining and implicitly unwrapped optionals. These are a few more tricks to get values out of optionals, while keeping Swift code much cleaner than its predecessors. Optional chaining makes it easy to drill into multiple levels of optional values. Implicitly unwrapped optionals, while not as safe as normal optionals, are often used under the assumption that the value that it is describing is never, or usually not, nil.