Time to start moving away from pure Swift posts, and start talking about making iOS apps. Though we will be talking about doing so with Swift, of course. Today we are going to talk about the Five Execution states of an iOS app. The iOS operating system itself changes which state your app is in, and gives you a chance to make appropriate changes during most of the transitions.
[Read more…]
Archives for October 2014
Generic Types in Swift
We have previously discussed generic functions on this blog in the post Generic Functions in Swift. There is one more aspect to generics in Swift that we have not covered yet, Generic types. Generic types let you write a named type (class, enumeration, or structure) that can accommodate different types in its implementation.
Now, that just sounds silly right there, why would you want to handle other types in your type? Well, the main and most predominant reason is for collections. The best example of this, is the built in Array and Dictionary types. The shorthand is a lot more readable, and is the recommended way to create Arrays and Dictionaries, but really, [Int] and [String : Int] are actually Array<Int> and Dictionary<String, Int> under the hood. See those angle brackets? Yep, those are the same kind of angle brackets used in generic functions.
[Read more…]
Operator Overloading — Tailor Swift To Your Needs
Sorry, but I have been waiting for months to make this pun. So today, we are going to talk about Operator overloading in Swift. This tool is very useful, but quite dangerous as well. The “With great power comes great responsibility,” quote is very appropriate for operator overloading. It can make your code a lot more concise, making even a function call seem like a 3-hour long lecture. But with that, you can also make the code nigh-unreadable to anybody that is new to your code.
Be very careful with this tool, and use it where it make sense, such as how “adding” two Swift Strings together makes a new string with one string first, then the other afterwards. You can make the addition operator print to the screen, make a network request, play music, or whatever else you could write a function for, but doing any of those would be a terrible idea in production code. You should do only what is necessary for the operator and nothing else. If you need different things like those, make an appropriately named function that makes it obvious that it will make a network request.
Whenever you think of whether to use operator overloading, you have to ponder whether it helps your code anymore than a simple function call. The function call may be longer, but it is a whole lot more expressive as to what it does (if named appropriately). If it looks like you are adding, subtracting, checking equality, or whatever else the built-in operators do though, and you do it enough in your code, Swift’s operator overloading may be the right tool for the job.
[Read more…]
Structures in Swift
We’ve so far looked at 2 of the 3 named types in Swift with their own dedicated posts. First was Enumerations in Swift, and later was Classes In Swift — An Introduction. Now we finally talk about the third one, Structures.
Swift Structures are Value Types
Let’s just get that out there right at the beginning. Much like the enumeration, structures are a value type in Swift. That means that each time a structure is assigned to a constant, assigned to a variable, or passed to a function (which really is just assigning it to a constant (usually)), it is copied. So if you set the same structure to multiple variables, and then modify one, all of the others will not change, just that one, because each new variable was a copy of the original, not a reference.
[Read more…]
Tuples in Swift: Create, Read, and Return
I was looking for a bit of information on Tuples in Swift for an app I was working on, and decided it would be best to gather what I learn into one, easy to access place.
Tuples are a compound type in Swift, which basically means that they can hold multiple values. A compound type can either contain what are called “named types”, which include classes, structures, and enumerations (also protocols, but since they don’t store values directly, I felt I should mention them separately), as well as other compound types. That means a tuple can hold other tuples. Another compound type a tuple can contain is the “function type,” which is a different way of referring to the type that describes closures (specifically the “() ->() ” style of type, which functions and methods also conform to. That also goes the other way, that a function type can hold other compound types, like the Tuple, and other closures, which we’ve seen in my previous post Closures and Capturing Values in Swift.
While technically inaccurate, you can conceptually think of Tuples kind of like a class or structure that you can make on the fly, without having to define a full fledged class or structure (nested or otherwise). According to Apple’s iBook though, they should probably only be used for temporary values, like to return them from a function. It doesn’t go into detail why, but if I had to guess, they were probably optimized for quick creation, at the cost of how it stores them. Nonetheless, one of the great new capabilities of Swift is multiple return types, and tuples are what Swift uses to achieve this, since they are technically returning a single value (the tuple itself).
One thing people are sometimes curious about tuples is how it is pronounced. Well, according to Dictionary.com, there are two valid pronunciations. In dictionary pronunciation code: /ˈtjʊpəl and ˈtʌpəl/. For anybody curious, ʊ is the sound of the “oo” in “took”, ə (a schwa) is how the a sounds in “about”, and ʌ (a wedge) is the sound of the “u” in “gut.” That means that those are pronounced (roughly) as too-puhl and tuh-puhl, respectively. I was digging around for examples of where they were from, only to find out both pronunciations happen for those examples as well, but they are basically from the end of words like “quadruple,” “quintuple,” and “octuple” (notice the “tuple” at the end of each). I personally prefer too-puhl, myself.
With readers coming from the US, the UK, and many other places, I suppose the pronunciation will vary. For those that listen to the Accidental Tech Podcast, apparently Dictionary.com agrees with both sides: huhv-er (/ˈhʌv ər) and hov-er (ˈhɒv-/ ər). For those of you that don’t, you really should go check it out.
Anyway though, you didn’t come here to learn about English phonetics, it’s time to learn how to use tuples in Swift!