Now, with knowledge of Protocols in Swift in hand, let’s get to talking about one of the other big additions to the Swift language: Generics. I had previously discussed in my post Swift Optionals – Declaration, Unwrapping, and Binding how Swift optionals work under the hood. In that case, the OptionalValue enum is a generic type. Today we are going to cover a slightly easier aspect of generics, generic functions. Generic functions are functions that can take parameters of any type (with or without constraints based on protocol adoption), and perform some action with them.
[Read more…]
Protocols in Swift
I wanted to write a bit about Generics in Swift, but I realized that some of the major powers of Generics require the use of protocols, so I felt I should start by talking about them first.
In Swift, protocols are basically a named contract that your classes can conform to. If your class says it conforms to Equatable, then it better fulfill all of the required functionality to make it equatable. They are rather similar to interfaces in C# or Java. They list off the function prototypes and variable declarations, as well as stating whether they are required or optional, but don’t actually do anything. It is up to class, structs, or enumerations that claim to conform to the protocol to actually provide the functionality.
Defining a Protocol
This is pretty easy. It is very much like defining a class in Swift, except you write protocol instead, observe:
protocol Vehicle { //Functions and properties prototypes go here. }
Then you just fill it with what you want.
[Read more…]
Nil Coalescing in Swift
Xcode Beta 5 was released with several good changes. I don’t want this to just be a “What’s new in Xcode Beta 5” post, because once Beta 6 is released, much less the real language, the only point for a post like that is history. So I am mostly going to talk about my favorite addition to Xcode 6 about optionals, with a short mention of a change that affects it. This great operator is still available in Swift 2.2 (Xcode 7.3) as well!
Nil Coalescing Operator
I did not see this one coming, but I do like it. It basically is a way to easily return an unwrapped optional, or a default value. As with many other parts of optionals, this operator is composed of question marks. Below is a simple example:
var someOptional: Int? = nil var aDefaultValue = 42 var theAnswer = someOptional ?? aDefaultValue
Since someOptional is nil (we didn’t set it to a valid Int yet), theAnswer will of course be 42.
[Read more…]
Enumerations in Swift
I have mentioned enumerations in three previous posts (Computed Properties in Swift, Swift Optionals – Declaration, Unwrapping, and Binding, and Loops, Switch Statements, and Ranges in Swift). It is probably about time to actually talk about them, eh? In C, and even Objective-C, enumerations were little more than glorified aliases for integer values. In Swift though, enumerations have been given significantly more power. In Swift, enumerations are a lot more like classes or structs, on top of the actual enumeration values themselves. For the moment though, we will talk about what makes an enumeration an enumeration.
[Read more…]
Loops, Switch Statements, and Ranges in Swift
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…]
- « Previous Page
- 1
- …
- 8
- 9
- 10
- 11
- 12
- …
- 16
- Next Page »