Using the Swift 4.2 Timer class in Xcode 10

Developers often need to do something many times and at regular intervals. One common application of such a use case is checking some process’s or device’s status, a technique called “polling.” In this tutorial, I’ll show you how to use the Swift 4.2 Timer class in Xcode 10 to repeatedly perform a simple task, every second, for 1 minute (60 seconds) — and update the user interface (UI) on each “tick.” You’ll learn how to start a timer ticking, pause it, resume it, stop it, and do something on each tick. Here’s what we’ll create during this tutorial — and the source code is available for download from GitHub:

To emphasize that the UI remains responsive when a timer is used judiciously, I added a UISlider to my storyboard and tested my code. Here’s the result:

Continue reading “Using the Swift 4.2 Timer class in Xcode 10”

The fix to Mojave slowing down to a crawl and freezing

If you’re one of the brave people who’ve installed Apple’s new Mojave (macOS 10.14) operating system, you may be experiencing extreme sluggishness and steady slowdowns after boot until everything just freezes up and you can’t do anything. It’s really exasperating, but there’s a cure for the freeze-ups… If your Mac’s primary drive has multiple partitions, and/or you have an external drive attached, and/or your external drive has multiple partitions.

Continue reading “The fix to Mojave slowing down to a crawl and freezing”

Swift 4.2: How the weirdest error message led to reimagining implicitly unwrapped optionals

In this tutorial, I’ll show you how researching and learning about the current programming language you’re using (i.e., Swift) can lead you to new discoveries, a better understanding of the language, and becoming a better problem solver. Recently, I ran into a Swift error that at first had me scratching my head. The error had to do with implicitly unwrapped optionals. My debugging and contemporaneous research led me to the little-known fact that, in Swift 4.2, a proposal “to remove the ImplicitlyUnwrappedOptional type from the Swift type system and replace it with an IUO attribute on declarations” was accepted and implemented. Despite the fact that I followed and covered language changes leading to Swift 4.2, I missed this seemingly innocuous but nonetheless prescient modification.

Continue reading “Swift 4.2: How the weirdest error message led to reimagining implicitly unwrapped optionals”

Some best practices for designing and coding Swift classes

My original article — “Best Practices for Building Swift Classes” — was published on appcoda.com.

Follow along as I write this tutorial’s Swift code in a playground! Download from GitHub.

In this tutorial, I’m going to show you some best practices that will help you design and implement classes (reference types) and then safely leverage reference semantics in Swift. Protocol-oriented programming (POP) and value semantics are all the rage now, but a promising new technology doesn’t mean you should throw all your classes away. Why not add some simple constructs to your classes like copy initializers, default initializers, designated initializers, failable initializers, deinitializers, and conformance to the Equatable protocol? To get real about my sample code, I’ll adopt these constructs in some classes and show you my best practices working in real life for drawing in your iOS app interfaces.

I’ll walk through the process of creating several protocols, creating classes that adopt those protocols, implement inheritance in these classes, and use instances of the classes (objects), all to illustrate my best practices — and to show some of the extra steps you may have to go through when working with classes.

Continue reading “Some best practices for designing and coding Swift classes”

Building a generic doubly linked list using protocol-oriented Swift 4

My original article — “Protocol-oriented Data Structures in Swift 4: A Generic Doubly Linked List” — was published on appcoda.com.

Follow along with this tutorial! Download the Swift 4 code from GitHub.

Let’s talk about creating a list on steroids, i.e., a generic doubly linked list in Swift. For our purposes here, a list is a software receptacle that contains related data that we’re interested in inspecting, organizing, manipulating, etc. A doubly linked list stores a list of “nodes.” Each node contains data, knows about the preceding node in the list, and knows about the following node in the list. We’ll talk about adding nodes to the list, removing nodes from the list, displaying information stored in nodes in the list, and traversing the list. I’ve used the term generic because you’ll see that I can store store pretty much every built-in or custom Swift type in my linked list, like Double, UINavigationController, Int, CGFloat, UIView, CGAffineTransform… You can even store a collection of instances of a custom class or struct in my list (see section “Storing custom types” below). Most importantly, I’ll show you how to move towards generic programming, also known as generics, parametric polymorphism, templates, or parameterized types, where, when possible, we can write code that applies to many types, and thus reduces code redundancy.

Continue reading “Building a generic doubly linked list using protocol-oriented Swift 4”

Self versus self in Swift 4 – capital “S” and lowercase “s”

RELATED: Find out how to make a copy of a class instance (object) using a C++-like copy constructor — a copy initializer in Swift.

Those of you who’ve used Objective-C and Swift for any meaningful length of time must be familiar with the self property of structs and classes. I’m not sure how many are aware of the Self “type” (sometimes called a “requirement”). I would be very interested in knowing how many understand the difference between self and Self. I’m talking about self with lower-case “s,” which I’ll call “small self” herein. It’s pretty well documented. Similarly, Self with an upper-case “S,” is what I’ll call “tall self” herein. It’s not very well documented.

Continue reading “Self versus self in Swift 4 – capital “S” and lowercase “s””

Class copy constructors in Swift 4 for defensive copying

Swift tutorials by iosbrain.com Notice that Swift almost seems to frown on making a copy of a reference type, i.e., a copy of an instance of a class, or, as some would rather put it, getting a copy of an object. I’m not talking about getting another reference to a class, I’m talking about getting an entire, separate copy of a class instance. This frowning on class copying is not an accident. Swift’s language architects want the syntax and semantics of the language to be crystal clear. They want developers to be confident that reference types and value types will both have 1) distinct and obvious meanings and that both types will 2) behave consistently. But still, why not be able to safely make a copy of a class instance? I’ll show you how in this tutorial by borrowing the copy constructor concept from C++. In Swift, we’d call this a “copy initializer.” (NOTE: Yeah, yeah, yeah, I know about NSCopying in Cocoa and Objective-C.)

Continue reading “Class copy constructors in Swift 4 for defensive copying”

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 (including betas)

FLASH: Updated 11/9/22 with a tutorial on how to obtain installers for macOS Ventura 13.0 and many older macOS versions.
FLASH: Updated 10/25/18 with NEW INSIGHTS on how to deal with the prohibitory symbol. When installing the macOS Mojave 10.14 beta 11 and then the public macOS Mojave 10.14, I ran into the prohibitory symbol again, actually twice during both installations, but got through to the final install in both cases. You should read this entire article, but if you’re in a hurry, jump to the section covering working around hitting the prohibitory symbol twice.

TOP NEW TIPS
  • If you get the prohibitory symbol multiple times, you may have to shut down and restart your Mac multiple times.
  • Boot into another APFS-based partition, like High Sierra (macOS 10.13), run Disk Utility, and then run First Aid on the Mojave partition. If Disk Utility reports that it fixed the Mojave partition — or even if it just gave the partition a clean bill of health — I’ll bet your problems will be solved.

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