iOS. Apple. Indies. Plus Things.


// Written by Jordan Morgan // Apr 11th, 2015 // Read it in about 2 minutes // RE: Swift

This post is brought to you by Emerge Tools, the best way to build on mobile.

Primitives, otherwise known as value types, are generally not that exciting.

Int, float, double, what have you. Programmers need them to play their small part in the grand scheme of things. Leave it to reference types to do the heavy lifting — the calculations, network calls, the big boy stuff.

In swift, Int grew up from a typedef and morphed into a struct behind the scenes — and they are actually kind of awesome. This week, let’s take a quick look at what this 64-bit signed integer can do.


Init is about the last thing one would normally associate with an integer. In Objective-C, programmers utilized the useful NSNumber to get cute with arithmetic operations. NSInteger, NSUInteger, int…they could do what they could do and that was it until NSNumber scooped them up in its loving arms.

That’s far from the truth in swift. The move to Int as a struct affords it several advantages than we’ve not had in the past. Int is the default return type swift one will receive when a variable is initialized with an integer literal:

let intLiteral = 1

Heck, Int can even take in a double or float in its initializer:

let twentyTwoPointFour = 22.4

let intRepresentation = Int(twentyTwoPointFour)

There are well over 15 ways to initialize an Int in swift, ranging from the far obscure use case to the incredibly useful.

Int Members

Moreover, swift continues to flex its proverbial muscles with Int through the use of its members. There are both clever instance members to pair along some static ones as well.

Need to know the maximum allowed value an Int can withstand? Perhaps the minimum? Swift, if anything, saves you a trip to Google via a quick stop in a playground file and Int’s static members:

let intMax = Int.max

let intMin = Int.min

//Empty bitset (because of course)  
let allZero = Int.allZeros

Not to be shown up by those shiny static values, Int has a bevy of useful information hanging out in its instance members as well. From a plain description to a hashValue, Int has everything one would need to know about it:

let plainOldInt = 1

//Opaque value  

//Changes byte order if needed - 72,057,594,037,927,936  

//Ditto - 1  

//Current Int with the btye order swapped - 72,057,594,037,927,936  



That’s a lot of information that a typedef could not give you by itself in Objective-C. When Apple said swift was to be modern, they clearly meant it.

Instance Functions

The advantages of manifesting an Int as a struct continue to pile up. Should one need to perform any mathematical operation on an Int (or otherwise) beyond typical addition/substraction/divide/multiply — Int’s instance methods are happy to oblige.

They range from the elementary:

let plainOldInt = 1



to middle school:

let plainOldInt = 1



…and High School:

let plainOldInt = 1



all the way to grad school:

//Runtime reflection!  
let plainOldInt = 1  
let type = plainOldInt.getMirror()






//Enum Value  


Though the MirrorType protocol is barely documented, it’s easy to use and understand. Perhaps I’m drinking the swift kool aid, but the fact that Ints can provide reflection out of the box, well, that gives me butterflies.

But, as my good friend Tim said, if you need to perform reflection on an Int at runtime — you’re probably doing it wrong. Right though he is, it’s impressive none the less.

Let’s also take a minute to apperciate Int’s static functions used primarily for determining arithmetic overflow:

let plainOldInt = 1

//(.0 2, .1 false)  
Int.addWithOverflow(plainOldInt, 1)

This static method also supports subtract, multiply, divide, and remainders varieties.

Wrapping Up

Int wields quite the utility belt that’s just aching to be utilized. It’s no longer shackled to another class to perform meaningful operations. Let’s also not forget the fun things you can do with it via extensions. Much like swift, Int is shaping up to be quite a useful tool to forge the next generation of iOS apps.

Until next time ✌️.


Spot an issue, anything to add?

Reach Out.