Kasper Kasper - 5 months ago 27
Swift Question

How to check if a String is an Int in Swift?

I have an TextField and I want to verify that the text of the input is an integer. How would I do this?

I want to write a function like this:

func isStringAnInt(string: String) -> Bool {

}

Answer

Use this function

func isStringAnInt(string: String) -> Bool {
    return Int(string) != nil
}
Comments