As we’ve seen in my article on Swift’s guard Statement and this article on error handling, Swift’s guard and throw statements encourage a style of programming where we return early from a given scope in case of error.In the case of the guard statement this is due to a failing precondition and in the case of the throw statement this is because we encountered some sort of … [Read more...]
Error Handling in Swift
Regardless of your skill, handling errors is a common task for any developer. As developers we have complete control of the code we write and features we include, but despite our best efforts, we don’t control everything. We don’t control the values our users enter or whether our applications have access to the resources they need. In this post we’re going to look at how we can … [Read more...]
Swift Type Aliases
Just a quick post today looking at type aliases in Swift.Type aliases, as the name suggests, are a way of providing an alternative name or alias for an existing Swift type.As such, type aliases provide a convenient way for referring to existing types using a name that is more appropriate to the particular context we’re working with and in doing so can make our API’s more … [Read more...]
Using Custom Types as Enumeration Case Raw Values in Swift
As mentioned in my previous post, the raw values used with enumerations in Swift are limited to either String, Character, Int, Float or Double values. In certain cases though we might want to use our own custom types. In this post I thought I'd investigate whether it was possible to use custom types of our own as raw values.Let’s start off with an example. Say we had a … [Read more...]
Swift Enumerations
In this article we’re going to continue our journey through the Swift programming language by looking at another of it’s features - enumerations.Enumerations, or ‘enums’ for short, are a common feature in many programming languages providing a convenient way of grouping a set of related values together into a single code construct. As we’ll see though, enumerations in Swift … [Read more...]
Swift Assertions
There is a term in software development - defensive coding - it is an approach to software design and development that tries to ensure that an application continues to function despite unexpected events such as actions by users or changes in the environment in which the app is running. The thing is, despite employing these techniques, situations can, and sometimes do, still … [Read more...]
Swift Strings
It’s been a while, but in the last post, we looked at the Unicode, the international standard for encoding, representing and handling, text. In this article, we build on that knowledge and look at how we can handle, process, and manipulate, characters and strings in Swift. Let’s start by looking at the smallest building block of most text processing - the character.String … [Read more...]
An Introduction to Unicode
Whether it be log statements, text labels, the words in a book or a post to social media, pretty much all Swift code we write deals with text in some form or other and having a detailed and clear understanding of how characters and strings works in Swift is critical.In the next two articles my aim is to help you gain this understanding and by the end of them, I’m hoping that … [Read more...]
Swift Sets
In Swift there are three primary collection types - arrays, dictionaries and sets. We’ve already covered arrays and dictionaries in previous posts and in this post we're going to complete the set (no pun intended) by looking at sets.What is a Set?In Swift, sets are unordered collections of unique values. This means that like a dictionary, the values held in a Swift set … [Read more...]
Pattern Matching in Swift
The Swift Programming language has a number of powerful features that are sometimes overlooked by those new to the language. In this article, we’re going to look at one such feature - pattern matching - which offers huge potential for both simplifying your code and making it much more expressive. … [Read more...]