What is the replacement for isDigit() for characters in Swift?

前端 未结 6 1135
失恋的感觉
失恋的感觉 2020-12-29 03:58

As mentioned in this post, before Xcode 6 Beta 4, one could use c.isDigit() and c.isAlpha() to find if c : Character was a digit or al

6条回答
  •  时光取名叫无心
    2020-12-29 04:44

    Swift 3 seems a little easier:

    let str = "abcdef12345"
    let digitSet = CharacterSet.decimalDigits
    
    for ch in str.unicodeScalars {
        if digitSet.contains(ch) {
            // is digit!
        }
    }
    

提交回复
热议问题