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

String Extension & Computed Property

You can also add to String a computed property.

The logic inside the computed property is the same described by OOPer

extension String {
    var isInt: Bool {
        return Int(self) != nil
    }
}

Now you can

"1".isInt // true
"Hello world".isInt // false
"".isInt // false

Leave a Comment

tech