Concurrency in iOS: serial and concurrent queues in Grand Central Dispatch (GCD) with Swift 4

Today, I’ll show you how to use Swift 4 and the Grand Central Dispatch (GCD) application programming interface (API) to implement the execution of (multiple) tasks in the background, i.e., parallel/concurrent execution of tasks on a multicore CPU. I’ve built a sample app that gives you two options: 1) synchronous execution of tasks in the background and 2) asynchronous execution of tasks in the background. All my Swift 4 code from this article, part of an Xcode 9 project which builds a fully-functional working sample app, is available for download here. Join me in: reviewing concurrent programming concepts; reviewing my concurrent Swift 4 code; and, examining videos of my app in action, videos of console output from my app, and the console output text itself. I’ll even show you how to graphically visualize my app’s CPU and thread usage with Xcode’s Debug Navigator.

This is a look at the app — a snapshot — after all images have finished downloading asynchronously in the background:

Here’s a video of the app downloading images asynchronously in the background:

Press the play button if you missed the first showing

Continue reading “Concurrency in iOS: serial and concurrent queues in Grand Central Dispatch (GCD) with Swift 4”

Xcode: using the “Debug executable” checkbox to step through app release versions

Here’s an Xcode setting, but what does it do for developers?

There’s a checkbox named “Debug executable” on the “Info” pane for Xcode’s “Debugging Options in the Scheme Editor.” Why is there such a dearth of information on this checkbox, a “simple” Xcode scheme option? Apple has little to say about the feature. Information about it is scarce on the web. I’ve heard all sorts of different opinions about what the checkbox does or doesn’t do. (Some of this may be exacerbated by Apple releasing buggy versions of Xcode.)

I’ll discover and explain, using the scientific method, how the “Debug executable” checkbox works. You may be thinking this is much ado about one checkbox, but my purpose is to get you to think about and learn a lot about debugging with Xcode. The absolute best developers are the ones with great and instinctive debugging, nay, TROUBLESHOOTING, skills.

Continue reading “Xcode: using the “Debug executable” checkbox to step through app release versions”

Debugging: symbolicating crash reports manually (stack trace, backtrace)

Swift tutorials by iosbrain.comToday, we’ll talk about manually symbolicating iOS and OS X application “crash reports.” Why? When you hear about a crash in one of your apps from a customer, the first thing you should do is try to get a copy of the crash report. But there are times when you get crash reports that aren’t automatically symbolicated, or that you can’t symbolicate by dragging into Xcode, or are partially symbolicated. When not symbolicated, you’re reading numeric addresses when you want to be reading code, like your function/class names. There are workarounds and we’ll discuss one today. Download the sample Xcode 9 project written in Objective-C to follow along. What’s a crash report, anyway? According to Apple:

Continue reading “Debugging: symbolicating crash reports manually (stack trace, backtrace)”

Fix for IBOutlet, IBAction connections disappearing in Xcode 9

You’ve been working on your billion dollar app happily for days or weeks. It’s Monday morning, you open up Xcode 9 to get back to work and — dang, bummer — all your IBOutlet and IBAction connections look like they’ve been disconnected (see image below):

Continue reading “Fix for IBOutlet, IBAction connections disappearing in Xcode 9”

Xcode tidbits: Open as Hex, Open As Source Code, (Git) line endings, and text encoding

With this article, I’m starting a series about all the goodies — useful tools — that can be found in Xcode. Some of these tidbits are tools everyone knows about while others are barely documented to undocumented. For example, how many of you know that you can view, inspect, and debug all your Auto Layout constraints live during app execution using the “Debug View Hierarchy” Xcode feature? I discussed that feature in detail in this article, “Troubleshooting Auto Layout using Xcode’s Debug View Hierarchy.” Today, we’ll discover two editors that ship with Xcode, the “Open As > Hex” and “Open As > Source Code” editors, both only available by right-clicking on files in the “Project Navigator” to reveal a contextual menu.

Continue reading “Xcode tidbits: Open as Hex, Open As Source Code, (Git) line endings, and text encoding”

Understanding Swift 4 protocols and using them in your apps

NOTE: Learn all about protocol-oriented programming in Swift here, here, and here.

[Download two Xcode 9 playgrounds with full Swift 4 source from GitHub.]

We’re going to talk about “protocols” in the Swift 4 language today. I’ll explain them conceptually, and then we’ll start coding protocols with a simple example. We’ll then create our own versions of the Apple built-in Equatable and Comparable protocols, and apply them to two real-world classes, one for tracking financial securities and one for representing geometric lines/vectors. Finally, we’ll test our geometric “Line” class in a type of Swift playground that supports rendering user interface components (like UIView) live in the simulator. But first, please ponder the layman’s definition of the word “protocol” before moving on:

… The official procedure or system of rules governing affairs of state or diplomatic occasions. …

The accepted or established code of procedure or behaviour in any group, organization, or situation. …

A procedure for carrying out a scientific experiment…

Swift Protocols

Apple’s “The Swift Programming Language (Swift 4.0.3)” documentation states:

Continue reading “Understanding Swift 4 protocols and using them in your apps”

Building Swift 4 frameworks and including them in your apps (Xcode 9)

Let’s talk about Swift 4 frameworks, one method for packaging, reusing, and sharing code. We’ll build our own framework and then include it in our own app. We could’ve talked about static libraries, but frameworks offer more advanced features — and will let us expand on code maintainability options in future discussions. (If you want to have that static versus dynamic library discussion, read this excellent article, but we won’t be debating the topic herein.) From Apple (my emphasis added):

Continue reading “Building Swift 4 frameworks and including them in your apps (Xcode 9)”

Typical Git/GitHub workflow tutorial: configure, clone, commit, stage, push, pull, status

Introduction

Suppose your supervisor or customer gives you a new project to work on: “I want you to start helping out on my iOS project, ‘Blocks-in-Objective-C.’ Please get a copy of the code, make these changes, test, and then check your new code into the repo. By the way, we’re using Git for source control.” Have you used Git before? Do you know it well? (Does anyone truly understand Git?) Today, I’ll show you how to accomplish that task just assigned by your supervisor or customer.

Requirements

Since this is an iOS blog, I’m assuming you’ve installed Xcode and therefore Git is installed on your Mac. (If you need to install Git, click here.)

Continue reading “Typical Git/GitHub workflow tutorial: configure, clone, commit, stage, push, pull, status”

Swift 3 segues, unwind segues, storyboards, and view/navigation controllers

[Download Xcode 8.2.1 project with full Swift 3 source from GitHub.]

Today’s tutorial covers transitions — segues — from one source storyboard scene to another destination scene, and unwind segues leading back from destination to source… I created a project to help you follow along with this tutorial, written in Swift 3, against the iOS 10 SDK, and using the Xcode 8.2.1 IDE. Please download the project. The app produced by the project is shown in action in the following video. Please watch before continuing on:

Segues don’t exist in a vacuum. I’ve introduced a UINavigationController into the mix. Of course, you’ll see a few UIViewControllers. I’ve also used a UITableView and managed its complexity by breaking it into logical pieces by using Swift “extensions.” As you proceed, you’ll have to grasp concepts like Auto Layout and managing a table view’s data source.

Continue reading “Swift 3 segues, unwind segues, storyboards, and view/navigation controllers”

Swift 3, iOS 10, Xcode 8: finding, adding, and removing IBOutlets

I’m going to show you how you can create IBOutlets, find your existing ones, and delete them. There will be times when you need to delete outlets, for example, if you connected the wrong component, have to change your design, misspelled an outlet, etc. You can’t do iOS without doing IBOutlets. An outlet is a connection you must create to allow your user interface (UI) to communicate with your code. For example, if you have a UIView on a storyboard scene, like a UIViewController, you may want to change that view’s background color during app execution. You may also need the bounds or frame of the view. If you have a UIButton, you may want to dynamically enable or disable it depending on some runtime condition. Say you have two UITextFields, “username” and “password,” and a UIButton containing the text “Login.” With IBOutlets, you could write your code so that the login button’s isEnabled property becomes true only when the username and password fields are filled with data. You would also want the text contained in the username and password fields once the user taps “Login.” IBOutlets form a connection between UI components like UILabel, UITextField, and UIView and their backing view controller that manages their behavior and presentation.

Continue reading “Swift 3, iOS 10, Xcode 8: finding, adding, and removing IBOutlets”