Value of type CharacterSet has no member InvertedSet - Swift 3

1.5k Views Asked by At

I am trying to convert over swift 2 code to swift 3:

var customAllowedSet =  NSCharacterSet(charactersInString:"=\"#%/<>?@\\^`{|}").invertedSet

It throws this error:

Value of type CharacterSet has no member InvertedSet
2

There are 2 best solutions below

0
On BEST ANSWER

According to Apple documentation the name of invertedSet method in Swift 3 was changed to just inverted. So try this:

var customAllowedSet =  CharacterSet(charactersIn:"=\"#%/<>?@\\^`{|}").inverted

PS: init(charactersInString:) of CharacterSet also changed to init(charactersIn:)

5
On

In Swift 3 you are also supposed to drop the NS prefix. You should use CharacterSet instead of NSCharacterSet. You should also declare it as a constant.

let customAllowedSet =  CharacterSet(charactersIn: "=\"#%/<>?@\\^`{|}").inverted

You can also declare it as a static property extending CharacterSet as follow:

extension CharacterSet {
    static let customAllowedSet = CharacterSet(charactersIn:"=\"#%/<>?@\\^`{|}").inverted
}

CharacterSet.customAllowedSet