Learn Swift and iOS Development
Master iOS development through in-depth tutorials and comprehensive courses on Swift, SwiftUI, UIKit, Core Data, and more.
Master iOS development through in-depth tutorials and comprehensive courses on Swift, SwiftUI, UIKit, Core Data, and more.
The final keyword in Swift is often ignored or overlooked. That isn't surprising as it isn't immediately obvious what the benefits are of annotating a class, method, or property with the final keyword. In this episode, you learn about the obvious and less obvious benefits of diligently marking a class, method, or property as final.
in Fundamentals
8:26
Enums have a wide range of applications and some of them are not obvious until you learn about them. In this episode of Writing Elegant Code with Enums, you learn how you can use enums to increase type safety and eliminate stringly typed code.
in Fundamentals
7:18
I love working with enums in Swift. While that may sound odd, I feel enums are undervalued and developers miss out on some of their nicer applications, especially when combined with associated values.
in Fundamentals
6:40
Types are a fundamental aspect of the Swift language. Swift's type system was pretty easy to understand in the early days. As the language matured, the type system gained in complexity to increase its flexibility. That flexibility came at a cost, though. Today's type system is pretty advanced and some aspects make your head spin.
in Fundamentals
7:39
Most of the Cocoa APIs we use to build applications are driven by Objective-C. This doesn't mean we need to use Objective-C to take advantage of these APIs, but it does mean that the APIs lack some of the niceties you expect from a Swift API.
in Fundamentals
13:46
I tend to talk about topics that aren't very exciting to most developers, but I also try to make it a habit to discuss topics that are fundamental for software development. Security ticks both boxes. Few developers get excited when I bring up security. The truth is that security should always be top of mind when you are creating software.
in Fundamentals
3:58
Many developers still get confused when they come across the word asynchronous. What does it mean? What is asynchronous programming? To understand what asynchronous programming is, we first need to understand its counterpart, synchronous programming. Let's use a playground to illustrate the difference.
in Fundamentals
16:13
In the previous episode, we drastically improved the performance and usability of the application. It no longer takes several seconds for the application to become responsive. Even though the application's performance has gotten better, there's room for improvement.
in Fundamentals
9:29
Even though threading and concurrency are more advanced concepts, you need to understand the basics regardless of your level of experience. The devices we develop applications for are powered by multicore processors and it's important to take advantage of that power.
in Fundamentals
12:02
It can be challenging or even overwhelming to prepare for a job interview or project meeting. The number of subjects you need to be familiar with as a developer is staggering. Mastering the fundamentals of the platform you're developing software for should always be a key focus area. That is why a significant portion of the content published on Cocoacasts focuses on the fundamentals of Swift and Cocoa development.
in Fundamentals
Every developer new to Cocoa and Swift development needs to become familiar with Apple's lingo and processes. In this episode, I'd like to talk about App IDs and bundle identifiers. What is an App ID? What is a bundle ID? What is the link between App IDs and bundle IDs?
in Fundamentals
Earlier this year, Parse announced it will retire its services in January of 2017. The announcement took many people by surprise, including me. When Facebook acquired Parse in 2013, many thought the future of the company looked bright. How wrong we were to think the acquisition was a guarantee for a long life.
in Fundamentals
What are unary, binary, and ternary Operators? The answer to this question is surprisingly simple.
in Fundamentals
What is the difference between a unary, binary, and ternary operator? Someone might ask you this question during your next job interview. In this tutorial, you learn about unary, binary, and ternary operators, and I show you a few examples. Let's start with unary operators.
in Fundamentals
Adding a URL scheme to your app requires only a few steps, but there are a few details to keep in mind. How your app handles deep links depends on a few factors. Does your app use UIKit or SwiftUI for its user interface? Is your app scene-based or not? We cover each scenario in this tutorial.
in Fundamentals
In today's tutorial, you learn how to convert an array to a string in Swift. The Foundation framework defines a few APIs we can use. Which API you use largely depends on the type of the elements of the array. Let's get started.
in Fundamentals
I recently came across a question from a developer that inspired me to write this tutorial. The developer ran into the following compiler error. Argument type String expected to be an instance of a class or class-constrained type. For someone new to Swift, this error can be quite challenging to resolve. Let's reproduce the error and track down the root cause. Once we understand the root cause, the solution is surprisingly simple.
in Fundamentals
Optionals are an integral aspect of Swift. Optional types are used to represent the absence or presence of a value. In other words, an optional type can hold either a value or nil, which indicates that a value is absent. When you declare a variable as optional, you tell the Swift compiler that the variable may not always have a value.
in Fundamentals
Sorting arrays is a common task in Swift. In this episode, you learn a handful of patterns to sort arrays, including sorting arrays of objects and sorting arrays by property. We use several functions that are defined in by Swift's Standard Library, such as sorted and sort. Are you ready?
in Fundamentals
Converting data to a string is common task in the context of networking or file input/output. In this episode, you learn how to convert a Data object to a String in Swift.
in Fundamentals
What does it mean for Swift to be ABI stable? ABI stands for application binary interface. ABI stability was introduced in Swift 5 and it means that the binary interface of the language is guaranteed to remain stable across different versions of the compiler and platforms.
in Fundamentals
Optionals are arguably the most challenging and frustrating aspect for developers that are new to Swift. What is an optional? What are the best practices for working with optionals? I explain this in detail in another post. This post zooms in on a very common error message developers run into.
in Fundamentals
Before I can explain the difference between instance methods and type methods, you first need to understand what an instance is and what a type is. Let me show you an example. Fire up Xcode and create a playground. Remove the contents of the playground and declare a struct with name User. The struct declares one constant property, name of type String.
in Fundamentals
A memberwise initializer is an initializer that is automatically generated by the compiler for structs that don't define a custom initializer in their declaration. That sound complicated. Let me show you a few examples to explain this in more detail. Create an empty playground if you want to follow along.
in Fundamentals
The nil-coalescing operator is represented by two question marks in Swift. It is one of the operators I miss most when working in Objective-C. The nil-coalescing operator isn't unique to Swift, though. Many languages have a nil-coalescing operator. Some language use the term null-coalescing operator. Let me show you how the nil-coalescing operator works.
in Fundamentals
The Foundation framework makes it painless to convert a string to a date, but there are a few pitfalls you need to watch out for. The class we use to convert a string to a date in Swift is DateFormatter (or NSDateFormatter if you are using Objective-C). I encourage you to follow along with me. Fire up Xcode and create a playground.
in Fundamentals
The short answer to this question is simple. A method is a function that is associated with a type, that is, a class, a struct, or an enum. This means that every method is a function, but not every function is a method. The long answer is more interesting, though.
in Fundamentals
Swift isn't a functional programming language, but you can use functional techniques in the code you write. Why is that? Swift has a number of functional features. For example, Swift has first-class functions and that opens up some interesting possibilities. In this post, you learn about higher-order functions and how you can use them in your projects.
in Fundamentals
Swift has first-class functions and that opens up a lot of possibilities. In this post, we explore function types. We also cover named type and compound types.
in Fundamentals
Even though Swift isn't a functional programming language, it has first-class functions. What are first-class functions and what does that mean for the Swift programming language? Don't let yourself be thrown off by the term functional programming. The concepts we cover in this post are not difficult to grasp.
in Fundamentals
Converting a date to a string isn't difficult in Swift. The class that makes this task painless is DateFormatter (or NSDateFormatter if you are using Objective-C). The DateFormatter class is easy to use. Let me show you an example using a playground.
in Fundamentals
Every Swift developer runs into this error at some point. Class 'ViewController' has no initializers. To resolve this error, you need to understand an important aspect of classes. Swift requires that the stored properties of a class receive an initial value during initialization. That includes the stored properties the class inherits from its superclass.
in Fundamentals
Swift's final keyword is often overlooked or dismissed as not necessary. Applying the final keyword is a subtle technique to optimize your code in a powerful way. Let me explain what I mean by that.
in Fundamentals
Storyboards are great for building user interfaces, but it can sometimes be frustrating to debug the cryptic warnings and errors Xcode throws at you. In this episode of Swift Help, I show you how to avoid unreachable view controllers when working with storyboards.
in Fundamentals
You are probably familiar with Swift's range operator. You can create a range in Swift by using the closed range operator or the half-open range operator. Take a look at these example to freshen up your memory.
in Fundamentals
What are unary, binary, and ternary Operators? The answer to this question is surprisingly simple.
in Fundamentals
Like many other languages, the Swift programming language defines an operator to calculate the remainder of a division. This is convenient and it works as advertised. Most developers wrongly assume Swift's remainder operator is identical to a modulo operator in other languages. This isn't entirely accurate, though. Let's take a look at the basics first.
in Fundamentals
At the time of writing, Swift defines a single ternary operator, the ternary conditional operator. As the name suggest, a ternary operator operates on three operands. An operand can be a value or an expression.
in Fundamentals
Navigation controllers are indispensable on iOS, tvOS, and, more recently, Mac Catalyst. The UINavigationController class is easy to work with. A navigation controller is an example of view controller containment. Every navigation controller manages a stack of view controllers. View controllers can be pushed onto the navigation stack or popped from the navigation stack.
in Fundamentals
The map(_:) method has the ability to transform a sequence of elements. Let me show you what that means. We define a constant, strings, and assign an array of String objects to it. The array of strings contains the elements one, two, and three.
in Fundamentals
Downloading images using Swift and the URLSession API isn't too difficult. The question is "Where do you store the images you download?" It is important that you know where you can and cannot store the images your application downloads. A safe place to store images is in the Documents directory of the application container. In this post, I show you how to store an image in the Documents directory using Swift. I promise you that it isn't difficult.
in Fundamentals
Almost every application needs to persist data in the application's sandbox. One option is to store data in the Documents directory of the application container. The question is How do you get the location of the Documents directory in Swift? This is easier than you might think.
in Fundamentals
Even though the UserDefaults class offers an easy-to-use API, I always create an extension for the UserDefaults class. Why is that? What are the benefits of an extension for UserDefaults? That is the focus of this post.
in Fundamentals
Developers that are new to Swift or user defaults are often confused why some of the data stored in the user's defaults database isn't persisted. To understand why and when this might happen, you first need to know more about the defaults system and how it stores data.
in Fundamentals
Many developers that are new to Swift and Cocoa development wonder what they should use, Core Data or the defaults system. I understand the motivation for this question, but it's the wrong question to ask. Core Data and the defaults system are very different persistence solutions. Both solutions can be used to persist data, but that is the only thing they have in common.
in Fundamentals
The title of this post is a bit misleading, because you should not store images in the user's defaults database. That is not what it was designed for. With this post, I hope I can convince other developers to use a different solution for storing images in the application's sandbox.
in Fundamentals
The UserDefaults class has a fairly extensive API that should meet most of your needs. From time to time, however, you need to perform a task that is less trivial. How do you clear the user's defaults database? The UserDefaults class doesn't offer this functionality. What options do you have to reset the user's defaults database in Swift?
in Fundamentals
Interacting with the defaults system is easy thanks to the UserDefaults class. You can store a range of data types in the user's defaults database, including strings, numbers and Date objects. The UserDefaults class is available on iOS, tvOS, macOS, iPadOS, watchOS and Mac Catalyst.
in Fundamentals
The UserDefaults class makes it trivial to store small pieces of data and persist them across application sessions. The problem is that only strings, numbers, Date objects, and Data objects can be stored in the user's defaults database. How do you store an enum in the user's defaults database in Swift? I answer that question in this post.
in Fundamentals
The UserDefaults class makes it straightforward to store data in the user's defaults database, but remember that only strings, numbers, Date objects, and Data objects are supported by the defaults system. Is it possible to store a custom object in the user's defaults database? It is, but it requires a bit of additional work.
in Fundamentals
Storing data in the defaults database is simple thanks to the easy-to-use API of the UserDefaults class. You can take advantage of the API on iOS, tvOS, macOS, iPadOS, and watchOS. In this post, I show you how to store a dictionary in the defaults database. I promise you that it isn't rocket science.
in Fundamentals
Storing data in the defaults database is simple thanks to the easy-to-use API of the UserDefaults class. You can take advantage of the API on iOS, tvOS, macOS, iPadOS, and watchOS. In this post, I show you how to store an array in the defaults database. I promise you that it isn't rocket science.
in Fundamentals
The UserDefaults class allows developers to interact with the defaults system, a simple key-value store. The defaults system is available on iOS, tvOS, macOS, iPadOS, and watchOS. The defaults database can only be used to store a predefined set of data types, strings, numbers, Date objects, and Data objects. It is also possible to store arrays or dictionaries of these types.
in Fundamentals
The defaults system is the easiest persistence solution on iOS, tvOS, macOS, iPadOS, and watchOS. A defaults database is nothing more than a key-value store. You interact with the defaults system through the UserDefaults class. Let's take a look at the API of the UserDefaults class.
in Fundamentals