Skip to content

Instantly share code, notes, and snippets.

@OctoberHammer
Last active May 27, 2017 19:53
Show Gist options
  • Save OctoberHammer/671a2c4705272299560e61f37c90ebde to your computer and use it in GitHub Desktop.
Save OctoberHammer/671a2c4705272299560e61f37c90ebde to your computer and use it in GitHub Desktop.
Convert from Any? into Int32?
//: Playground - noun: a place where people can play
import UIKit
import Foundation
//Пробовать с разными - раскоментаривать по очереди
var someString: String? = "bjbb"
//var someString: String? = "3456"
//var someString: Int? = 3456
//var someString: Int?
func CastToInt32(someId: Any?) -> Int32? {
var identifier: Int32?
if let stateCode = someId as? Int ?? Int(someId as? String ?? "") {
identifier = Int32(stateCode)
return identifier
} else {
return nil
}
}
let myInt32 = CastToInt32(someId: someString)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment