Skip to main content

Globals and Swift (Language)

We’re all aware that global variables are bad and that we shouldn’t do globals. Right? Well... Sometimes you need globally available information like user interface idiom or OS version. This information is available from UIDevice and you can grab it from anywhere in your app.

Testing Device Idiom and OS Version in Swift

if UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Phone { ... }
if UIDevice.currentDevice().systemVersion.compare("8.0", options: NSStringCompareOptions.NumericSearch) != NSComparisonResult.OrderedAscending { ... }

However, that’s a lot type type and isn’t easy to read either. In Objective-C we could use macros in the pre-compiled header to make application code easier to read and write.

ProjectName-Prefix.pch

#define isPhone ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
#define isIOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

Application Objective-C Code

 if isPhone { ... }
 if isIOS8 { ... }

But Swift doesn’t have macros. Swift does, however, have a global namespace. Anything in that global namespace can be access by any other file in the same module. So you can do something like this.

Globals.swift

let isPhone = UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Phone
let isIOS8 = UIDevice.currentDevice().systemVersion.compare("8.0", options: NSStringCompareOptions.NumericSearch) != NSComparisonResult.OrderedAscending

Application Swift Code

if isPhone { ... }
if isIOS8 { ... }

That’s much better. Using the global namespace to define easy to type and read constants for system provided information feels acceptable to me. Just remember not to abuse it you should be okay.

Comments

Popular posts from this blog

The Python Code Handbook (free book with 40 code examples)

Introducing the Python Code Handbook from FreeCodeCamp that are worth your time: 1. This handbook will teach you Python for beginners through a series of helpful code examples. You'll learn basic data structures, loops, and if-then logic. It also includes plenty of project-oriented learning resources you can use to dive even deeper. (full handbook):  https://www.freecodecamp.org/news/python-code-examples-simple-python-program-example/ 2. freeCodeCamp just published this course to help you pass the Google Associate Cloud Engineer certification exam. If you want to work as a DevOps or a SysAdmin, this cert may be worth your time. You'll learn Cloud Engineering fundamentals, Virtual Private Cloud concepts, networking, Kubernetes, and High Availability Computing. (20 hour YouTube course):  https://www.freecodecamp.org/news/google-cloud-digital-leader-certification-study-course-pass-the-exam-with-this-free-20-hour-course/ 3. React Router 6 just came out a few months ago, and freeCo...

How to Use OUIEditableFrame with the OmniGroup Framework

TLDR: This blog post provides a detailed tutorial on how to implement OmniGroup’s OUIEditableFrame in an iOS project to render text with proper kerning, which cannot be achieved with standard UIKit controls due to a bug. The guide walks through creating and configuring an Xcode workspace, cloning the OmniGroup project from GitHub, adding necessary frameworks and dependencies, and writing the required view controller code. Learn to implement OmniGroup's OUIEditableFrame for proper text kerning in iOS projects with this step-by-step Xcode tutorial. Table of Contents: Introduction Create and Configure Xcode Workspace Clone the OmniGroup Project from GitHub Add the FixStringsFile Project Add OmniBase Add the Rest of the Frameworks Write View Controller Code Making It Work in iOS 5 Get Rid of the Static Analyzer Message If you need a UITextField that renders text with proper kerning you don’t have many of options. Writing al...

Integrating SwiftUI into Existing AppKit Cocoa Projects: A Step-by-Step Guide

As Apple continues to push SwiftUI as the future of app development across its platforms, many developers find themselves in a position where they need to integrate this modern framework into their existing AppKit Cocoa projects. This integration can breathe new life into older applications, allowing developers to take advantage of SwiftUI's declarative syntax and powerful features while maintaining their existing codebase. In this guide, we'll walk through the process of adding SwiftUI to an existing AppKit Cocoa project, providing you with the knowledge to modernize your macOS applications.