Enable better performance in your app

decorative: a pattern of sweeping blue lines
decorative: a pattern of sweeping blue lines
Photo by Dan Gold on Unsplash

The @inlinable attribute is one of Swift's lesser-known attributes. Like others of its kind, its purpose is to enable a specific set of micro-optimizations that you can use to increase the performance of your application. Let's take a look at how this one works.

Inline Expansion in Swift With @inline

Perhaps the most important thing to note is that although @inlinable is related to code inlining, it's not the same as the @inline attribute. We'll go through the concepts of @inline before introducing @inlinable.

In programming, inline expansion, also called inlining, is a compiler optimization technique that replaces a method call with the body of the said method. …

Monitor all your OS-related events

Website logo
Website logo
Photo by the author.

The DispatchSource family of types from GCD contains a series of objects that are capable of monitoring OS-related events. In this article, we'll see how to use DispatchSource to monitor a log file and create a debug-only view that shows the app's latest logs.

Context: File Logging in Swift

While every app will print debug logs to the developer console, it’s good practice to save these logs somewhere. OSLog automatically saves your logs to the system, but I find that maintaining your own log file (like MyApp-2020-11-24T14:23:42.log) is an additional good practice. If your app receives a bug report from an external beta tester, you may find retrieving and inspecting your own log file easier than teaching that user how to extract and send their OSLogs. …

Discover 7 lesser-known types

Website logo
Website logo
Photo by the author.

Foundation provides you with a lot of the bread and butter needed for your daily iOS development, ranging from structures like Data all the way to complete APIs like URLSession. But as it turns out, we only use a fraction of what Foundation offers.

There are a bunch of Foundation types that are so situational that people doubt they even exist in the first place! In fact, they are so rarely mentioned as solutions to daily problems that developers may end up coding things that already exist in these SDKs. But they do exist, and although some of them are really old, most of these types are still very useful. …

How if #available works under the hood

People enjoying the view from a height.
People enjoying the view from a height.
Photo by Falaq Lazuardi on Unsplash.

We use API availability checks all the time to provide fallbacks for users running older iOS versions, but have you wondered how the Swift compiler handles this? In this article, we’ll take a deep dive into how the #availability condition works, how the Swift compiler is able to know if a specific symbol is available for usage, and what the code you wrote looks like after being optimized.

I have recently authored an evolution proposal to add a new #unavailable attribute to Swift, and while I haven't needed to do any meaningful work on Swift's availability system to implement it, this gave me the opportunity to learn a little more about things work deep down. …

Terminate your app in the middle of an XCUITest and launch it again somewhere else

Image for post
Image for post
Photo by Fotis Fotopoulos on Unsplash.

Did you know it’s possible to terminate your app in the middle of an XCUITest and launch it again from somewhere else? Let’s see how to use this trick to test that deep links and universal links are properly launching your app when executed from Safari or iMessage.

It’s tricky to test iOS features because while you can write unit tests to guarantee that your abstraction of it works, you can’t really unit-test for whether iOS will correctly call what you think will be called. In the case of deep links, what iOS does differs depending on the current state of your app (closed or in the background) and which delegates you support (AppDelegates versus SceneDelegates). …

Improve the memory usage of your apps

Image for post
Image for post
Photo by Sonja Punz on Unsplash.

Even if you don’t know what the Observer pattern is, there’s a good chance that you’ve applied it in the past. This design pattern is used to create APIs that notify one or more subjects about changes in a certain object, with the NotificationCenter being the most popular use of this pattern in iOS.

One simple way to reproduce what the NotificationCenter does is to create a dictionary that maps a string (a notification) to an array of closures. Whenever that notification is "posted," all of the closures are executed:

However, the point of this article is not to attempt to reproduce the NotificationCenter but to show you what this implementation implies. You must be aware that whenever you're using the basic Swift dictionaries, arrays, or sets, all keys and values are retained! Additionally, you need to be aware that closures are reference types, so they are retained as well and can outlive their owners. …

Protect your iOS apps from jailbreak tweaks

Image for post
Image for post
Photo by Maulik Sutariya on Unsplash

When jailbreaking of iOS devices first became popular, it was common for iOS developers to try to defend their apps from users that altered their devices to enable piracy. There were many ways of doing that, which included checking for the existence of Cydia, checking if the app could read files outside its sandbox, crashing the app if it detects a debugger, and more.

As time has shown, these defensive measures were and still are a really bad idea. If an attacker has physical access to the device, there’s no way you can trust your app’s logic. It was and still is trivial for hackers to pretend their devices are not jailbroken and effectively bypass these measures. …

Understanding Swift metatypes

Image for post
Image for post
Photo by Artem Sapegin on Unsplash

Ah, metatypes. That’s another one for the list of things I use everyday but couldn’t explain in an interview if my life depended on it.

Metatypes are pretty useful in Swift, and you have certainly used it on multiple occasions. Unfortunately they look pretty weird in code, which can cause some confusion when trying to understand what they really are.

I know how these weird suffixes can confuse you, but don’t worry, they’re actually pretty straightforward once you get to know the differences between each of them. But before getting into that, let’s take a step back.

What’s a Metatype?

If you take a look at Apple’s docs, you’ll see that a metatype is defined as being the type of a type. Wait, isn’t String a type? What could possibly be type of a String that’s already a type? SuperString?? …

Image for post
Image for post

While Xcode provides several visual abstractions for lldb commands like adding breakpoints by clicking the lines of code and running by clicking the play button, lldb provides several useful commands that are not present in Xcode’s UI. This can range from creating methods on the fly to even changing the CPU’s registers to force a specific flow on the app without having to recompile it, and knowing them can greatly improve your debugging experience.

Not all Swift things are developed in Xcode — things like the Swift Compiler or Apple’s SourceKit-LSP work better through other means, and these means usually end up having you use lldb manually.

Let’s look at Apple’s replacement for applicationDidFinishLaunching

Image for post
Image for post
Photo by Natalia Y on Unsplash.

In this article, we’ll see how Swift determines the entry point of an app, the different attributes used to alter this behavior in iOS, how they work internally, and what Swift 5.3’s new @main attribute brings to the table.

How Swift Scripts Work

When executing Swift code from the command line, by default, the code will be read from top to bottom just like in any other scripting language. While Swift scripts support everything you’d have in any other context, they have the additional advantage of being able to write expressions in a global scope:

print("Hello")struct MyStruct {
func foo() {}

In an iOS app, by default, doing so would result in the Expressions are not allowed at the top level error, but in a scripted world where you want to execute something as possible, there's no point in preventing this behavior. …


iOS Developer at Spotify | Follow me on Twitter at @rockbruno_ for more iOS tricks! Writer of https://swiftrocks.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store