Two Structural Design Patterns in Swift: Facade and Adapter

Swift tutorials by iosbrain.com My original article — “Design Patterns in Swift #3: Facade and Adapter” — was published on appcoda.com.

This tutorial is the third installment in my series on design patterns. I started with a tutorial examining two examples of patterns in the “creational” category: factory method and singleton. I then discussed two examples of patterns in the “behavioral” category: observer and memento. In this tutorial, I’ll explain two examples of patterns in the “structural” category: facade and adapter. I urge you to review my first two posts mentioned above so you can familiarize yourself with the concept of software design patterns. Beyond a brief reminder today of what constitutes a design pattern, I’m not going to regurgitate all the definitions again. All the information you need to get up to speed is in my first to tutorials, here and here.

Continue reading “Two Structural Design Patterns in Swift: Facade and Adapter”

How drawing works in an Xcode playground

How many of you use freeform drawing in Xcode playgrounds? How many of you understand how drawing in playgrounds work? Xcode playgrounds can serve as great tools for prototyping your in-development apps, whether it be experimenting with algorithms or toying with ideas for app user interfaces. Granted that drawing in playgrounds is not that well documented. So the subject of this tutorial is how drawing in Xcode playgrounds works and a good number of pointers to help you start drawing in playgrounds. Here’s an example of what I’m talking about:

Continue reading “How drawing works in an Xcode playground”

Fix for prohibitory symbol (do not enter, stop sign) when booting into or updating macOS Mojave 10.14 beta

Over the last week, I encountered the dreaded Apple “prohibitory symbol” — do not enter sign or stop sign — twice: once when just booting into macOS Mojave 10.14 beta 4 and again when updating to macOS Mojave 10.14 beta 8. Here’s what my MacBook Pro’s screen showed me:

The is one of the symbols that users of Apple products running macOS rightfully fear. Of course, we’re talking about products like the Mac Pro, Mac mini, iMac, MacBook, MacBook Air, and MacBook Pro.

When you see the prohibitory symbol, please don’t panic. Please don’t go through any lengthy reinstalls or plunge down the troubleshooting rabbit hole before you hear me out. Apple says you should “try reinstalling macOS using macOS recovery.” Another Apple support page suggests that you “revert to a previous version of macOS if you experience trouble after installing an update” (using Time Machine). Several Apple forums advise booting from a recovery hard drive, reinstalling macOS, repairing your hard drive with Disk Utility… These are all valid solutions… ones you should only use after trying something simple first.

Continue reading “Fix for prohibitory symbol (do not enter, stop sign) when booting into or updating macOS Mojave 10.14 beta”

Swift 4.2 improvements? Member/dot syntax for subscripts. Trying it out in a protocol-oriented, generic linked list.

The code shown herein will only compile and link in Xcode 10 beta and run on iOS 12 beta and/or OS X 10.14 beta.

While working on a Swift protocol-oriented and generic linked list, I got to thinking about Apple’s “improvements” to version 4.2 of their flagship language. Since a linked list is a list, I thought, “Why not add a subscript to my linked list to facilitate finding specific nodes in my list?” I did that in Swift 4.1 and got what most developers would’ve expected, e.g., used linkedList["node4"] to get the node in the list associated with the keyword “node4.” With Swift 4.2, I can use the controversial new @dynamicMemberLookup language attribute and implement dot/member notation, like linkedList.node4 to get that same node in the list associated with “node4.” Big improvement, huh? Well, maybe. We’ll talk about how this new and improved subscript is more than just about syntactic sugar, but that the “driving motivation for this feature is to improve interoperability with inherently dynamic languages like Python, Javascript, Ruby and others.” Note that all code shown in this tutorial was written in two Xcode 10 beta playgrounds.

Continue reading “Swift 4.2 improvements? Member/dot syntax for subscripts. Trying it out in a protocol-oriented, generic linked list.”

Xcode 9 playground error: No such module ‘UIKit’ (or ‘AppKit’)

While creating a new Xcode playground on my MacBook Pro today, I got the most bizarre error message: “No such module ‘UIKit'”. I was using Xcode Version 9.2 (9C40b). Yes, I know there are more recent versions, but I haven’t had the need to upgrade my MacBook Pro. Parenthetically, I do have Xcode 9.4.1 (9F2000) and Xcode 10 beta 6 (10L232m) loaded on my main development machine. I’ll share the solution to this problem with you in the hopes that you, like me, will learn something new about Xcode today.

Continue reading “Xcode 9 playground error: No such module ‘UIKit’ (or ‘AppKit’)”

Swift 4.2 improvements: #warning and #error compiler directives

The code shown herein will only compile and link in Xcode 10 beta and run on iOS 12 beta and/or OS X 10.14 beta.

We’re in the middle of Apple’s annual product upgrade cycle and this article is the second in a series of tutorials, started last week, meant to highlight the most important new features of Swift 4.2. Today, we’ll look at two two new Swift 4.2 features, the #warning and #error compiler directives.

Continue reading “Swift 4.2 improvements: #warning and #error compiler directives”

Two Behavioral Design Patterns in Swift: Observer and Memento

My original article — “Design Patterns in Swift #2: Observer and Memento” — was published on appcoda.com.

This tutorial is the second installment in a series on design patterns started last week. There are 23 classic software development design patterns probably first identified, collected, and explained all in one place by the “Gang of Four” (“GoF”), Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides in their seminal book, “Design Patterns: Elements of Reusable Object-Oriented Software.” Today, we’ll focus on two of these patterns, “observer” and “memento,” which fall into what the GoF calls the “behavioral” category. Follow along with the Xcode projects, both on GitHub, available for observer here and memento here.

Continue reading “Two Behavioral Design Patterns in Swift: Observer and Memento”

Swift 4.2 improvements to Collection and Sequence protocols with method allSatisfy

The code shown herein will only compile and link in Xcode 10 beta and run in iOS 12 beta and/or OS X 10.14 beta.

We’re in the middle of Apple’s annual product upgrade cycle and this article is the first in a series of tutorials meant to highlight the most important new features of Swift 4.2. Instead of trying to cover all of the 4.2 features/improvements in one very long article, I’m going go talk about each aspect of the new 4.2 version, one or two features at a time. (If you’re interested in more details as to why I’m focused on 4.2, see section “Swift version methodology” below.) Today, I’ll cover the allSatisfy(_:) instance method (see also here) of the Sequence protocol (see also here), of course intimately related to the Collection protocol (see also here).

Continue reading “Swift 4.2 improvements to Collection and Sequence protocols with method allSatisfy”

Controlling chaos: Error checking in Swift 4 with if let, guard, and failable initializers

Swift tutorials by iosbrain.com In this tutorial, the third in a series of tutorials, we’re going to finish the arduous topic of looking for unexpected values, events, and conditions that arise during program execution, using a technique I like to call “error checking.” Today, I’ll concentrate on nil values, optionals, optional binding, the guard statement, failable initializers, and finally, give you some advice about keeping your error checking code consistent, for example, when to use Swift “Error Handling” or when just to return true/false or use guard statements.

Continue reading “Controlling chaos: Error checking in Swift 4 with if let, guard, and failable initializers”

Two Creational Design Patterns in Swift 4: Factory Method and Singleton

My original article — “Design Patterns in Swift #1: Factory Method and Singleton” — was published on appcoda.com.

There are 23 classic software development design patterns probably first identified, collected, and explained all in one place by the “Gang of Four” (“GoF”), Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides in their seminal book, “Design Patterns: Elements of Reusable Object-Oriented Software.” This tutorial focuses on two of those patterns in terms of what the GoF calls the “creational” category: “factory method” and “singleton.” Follow along with the Xcode projects both on GitHub, available for factory method here and singleton here.

Continue reading “Two Creational Design Patterns in Swift 4: Factory Method and Singleton”