Swift 3 conversion: value of type 'characterset' does not have member character '

I am currently converting my codes into swift 3 and I am encountering the above error with the following codes.

 func containsAlphabets() -> Bool {
    //Checks if all the characters inside the string are alphabets
    let set = NSCharacterSet.letters
    return self.utf16.contains( { return set.characterIsMember($0)  } )
}

Any souls can help with this?

+4
source share
2 answers

You can do the following:

extension String {
    var containsAlphabets: Bool {
       return utf16.contains { (CharacterSet.letters as NSCharacterSet).characterIsMember($0) }
    }
}
+2
source

In Swift 3, it is CharacterSetre-designed to work well with UnicodeScalar, and not with, the UTF-8 code point.

In this case, you can write something like the following:

var containsAlphabets: Bool {
    //Checks if any of the characters inside the string are alphabets
    return self.unicodeScalars.contains {CharacterSet.letters.contains($0)}
}

Give it a try.

+6
source

Source: https://habr.com/ru/post/1655138/


All Articles