Be careful with Int and 32 and 64 bit architectures!

While I was doing a new App, I did all the development using Iphone 5s as the base model in the simulator, just testing it a few times om 6 and 6 plus. However, as I had submitted it to the appstore, I tried it on the 4s simulator, just to learn that the app crashed every time it should convert a String to an Int. This happened even though I had checked the string for nil and everything.

What happened was a bit annoying and scaring.

My code looked like this, a clousure making an int out of a string from a string where the int is embedded:

internal func makeUnixTimeExtracor(firstString:String, secondString:String) -> ( ( dsbDateString:String) -> Int){

func extractUnixDate( dsbDateString: String ) -> Int {

let startRange = dsbDateString.rangeOfString(firstString )
let endRange = dsbDateString.rangeOfString(secondString )
let startIndex = startRange?.endIndex
let endIndex = endRange?.startIndex

if( startRange != nil && endRange != nil){

let range = startIndex!..<endIndex!
let unixTimeString = dsbDateString.substringWithRange( range )
return unixTimeString.toInt()!


let dateIntervalNow = NSDate().timeIntervalSince1970
return Int(dateIntervalNow)


return extractUnixDate


The culprit was this one: return unixTimeString.toInt() On a 32 bit architecture is does NOT throw an exception, saying it can’t convert it to a 64 bit Int, but it just returns a “nil”, which later made my app crash. On 64 bit platforms, it ran perfectly.

The workaround was to make in a NSString and then convert that to a 32 bit int like this:

let unixTimeString = dsbDateString.substringWithRange( range )
return (unixTimeString  as NSString).intValue

Of course you must check that the value does not get any bigger than a 32 bit Integer.

Also this shows the importance og testing the code in all environments.