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

What is .csp extension? C++ Server Pages

C++ Server Pages C++ Server Pages (CSP) is a Web Engine for advanced Web Application Development, that uses blended Markup Language / C++ scripts ( such as HTML/C++, XML/C++, WML/C++ etc.) Similar to ASP and JSP, it provides a great easiness in creating web pages with dynamic content, as well as complex business applications. However, instead of Java, Javascript or VBscript, it uses C++ . This brings some significant advantages: Incredibly high processing efficiency. Benchmarks have shown a range of 80 to 250 times higher processing speed than ASP. The use of pure C++ allows the use of tons of libraries that are currently available. It is important to notice that the libraries written in C++ are tens or hundreds of times more than in any other language. It is widely accepted that the most skilled programmers in the IT market are the C++ ones. However, CGI, ISAPI and other frameworks where C++ applies, do not provide the web developer with facilities for efficient app

Sample Database in CI Wizard Demos Explained

In this video, the design of the sample relational database used throughout the demos of CodeIgniter Wizard is explained using an E-R diagram. Originally designed in Navicat ( https://www.arclerit.com/navicat/?ref=YToperand ) the E-R diagram shows the entities named 'countries', 'cities' and 'people' which are linked via foreign key definitions. The 'People' table contains about every kind of data column to demonstrate a use case for various data types, so the code generator happens to produce code for a variety of fields. To learn about the new features of CodeIgniter Wizard, you can view https://youtu.be/msSVRN5tY88 For an end-to-end, 9-minute demo, watch https://youtu.be/fo2wmzZ2p3I To take a closer look at what the generated code looks like, see https://youtu.be/6Gh1LU1pV-I APP STORE LINK https://apps.apple.com/app/codeigniter-wizard/id1531951619 PRODUCT WEB SITE https://www.ozar.net/products/codeigniterwizard/?ref=DAVElopment FAQ https

How to Cache NSDateFormatter?

Instantiating NSDateFormatter objects is slow. Most well-written apps that use NSDateFormatter will make efforts to reduce the number unique instances created. Using a static property is sufficient for most cases, but what if you work on a large app where lots of NSDateFormatter objects are needed by a large number of classes. Wouldn’t it be nice if there was an NSCachedDateFormatter that keeps track of what it has instantiated and will return cached objects and only instantiate new ones when necessary? Wouldn’t it be nice if NSCachedDateFormatter was a singleton and could be used app-wide? Here's a sample class that can be used to fulfill this need.