Unit Testing Dispatch Queues With Dependency Injection

September 30, 2018 | Swift, Networking, Unit Testing, iOS, macOS, tvOS, watchOS

We write asynchronous code when we know a task will take time to complete, whether because it's computationally expensive or we're making a network request. Testing this code can be difficult, especially when the asynchronous logic is internal. For example, let's say we're making a fire and forget request to load an image into an image view? We would likely make the request on a background queue and . . .

Convenient Sorting With Swift Key Paths

September 30, 2018 | Swift, iOS, macOS, tvOS, watchOS

Key paths were introduced in Swift 4 and allow you to reference a type's properties without evaluating them. In this tutorial, we'll use key paths to write a generic array extension that makes sorting more convenient with code that is easier to read.

Using Switch Statements And Case Let for Type Checking in Swift

September 8, 2018 | Swift, Control Flow, iOS, macOS, tvOS, watchOS

One thing that bothers many developers about using if statements for type checking in Objective-C is that it suggests a specific order of importance. For example, here's how you might use type checking in an Objective-C `prepareForSegue` method. This Swift version is much more concise, but we still have . . .

Making Views More Testable & Reusable

July 22, 2018 | Swift, Unit Testing, iOS, macOS, tvOS, watchOS

In this tutorial, we'll see how we can use protocols to make custom view subclasses more generic. The benefit is that they'll be easier to test and reuse, and our code will be easier to read and maintain.

Understanding Hashable, Equatable, and Set Membership

July 1, 2018 | Swift, Protocols, Collections, iOS, macOS, tvOS, watchOS

The `Hashable` protocol in the Swift Standard Library allows us to use our own custom types as a key in a dictionary or as a member of a set. Conforming to `Hashable` where appropriate can make our code safer and improve performance. However, it's important to understand how `Hashable` and `Equatable` work together . . .