intWritten by Jordan Morgan • Apr 11th, 2015
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 //22 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.
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:
//9,223,372,036,854,775,807 let intMax = Int.max //-9,223,372,036,854,775,808 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 plainOldInt.value //Changes byte order if needed - 72,057,594,037,927,936 plainOldInt.bigEndian //Ditto - 1 plainOldInt.littleEndian //Current Int with the btye order swapped - 72,057,594,037,927,936 plainOldInt.byteSwapped //1 plainOldInt.hashValue //"1" plainOldInt.description
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.
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 //2 plainOldInt.successor() //0 plainOldInt.predecessor()
to middle school:
let plainOldInt = 1 //9 plainOldInt.distanceTo(10) //11 plainOldInt.advancedBy(10)
…and High School:
let plainOldInt = 1 //1 plainOldInt.toIntMax() // plainOldInt.encode()
all the way to grad school:
//Runtime reflection! let plainOldInt = 1 let type = plainOldInt.getMirror() //1 type.value //ExistentialMetaType type.valueType //nil type.objectIdentifier //0 type.count //"1" type.summary //Enum Value type.quickLookObject //Ditto type.disposition
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.
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 ✌️.