You can check it like
func isStringAnInt(stringNumber: String) -> Bool { if let _ = Int(stringNumber) { return true } return false }
OR
you can create an extension for String. Go to File -> New File -> Swift File
In the newly created Swift file you can write
extension String { func isStringAnInt() -> Bool { if let _ = Int(self) { return true } return false } }
Thus, you can access this function throughout your project, for example,
var str = "123" if str.isStringAnInt()
source share