Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 conversion : value of type 'characterset' has no member 'characterIsMember'

I am currently converting my codes to swift 3 and i encounter the above mentioned 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 kind souls can assist on this?

like image 364
LuidonVon Avatar asked Sep 20 '16 04:09

LuidonVon


2 Answers

In Swift 3, CharacterSet is re-designed to work well with UnicodeScalar rather than UTF-8 code point.

In this case, you can write something like this:

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

Please try.

like image 165
OOPer Avatar answered Sep 21 '22 04:09

OOPer


edit/update: Xcode 11.4 • Swift 5.1

extension StringProtocol {
    var containsLetters: Bool { contains { $0.isLetter } }
}
like image 35
Leo Dabus Avatar answered Sep 22 '22 04:09

Leo Dabus