iosobjective-cnscharacterset

Implicit conversion of 'NSStringEncoding' (aka 'unsigned long') to 'NSCharacterSet * _Nonnull' is disallowed with ARC


on following code i'm get the error message: Implicit conversion of 'NSStringEncoding' (aka 'unsigned long') to 'NSCharacterSet * _Nonnull' is disallowed with ARC

Where i'm making wrong?

NSURL *candidatesURL = [NSURL URLWithString:[str_url
                                             stringByAddingPercentEncodingWithAllowedCharacters:NSUTF8StringEncoding]];

Solution

  • You are using the wrong method. Instead of using stringByAddingPercentEncodingWithAllowedCharacters use this stringByAddingPercentEscapesUsingEncoding

    If you wan't to use this method stringByAddingPercentEncodingWithAllowedCharacters you have to pass a set of allowed characters.

    Try this.

    NSURL *candidatesURL = [NSURL URLWithString:[str_url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    

    Or if you app development target is IOS 9.0 and above.

    stringByAddingPercentEscapesUsingEncoding is deprecated so you can use stringByAddingPercentEncodingWithAllowedCharacters and use URLHostAllowedCharacterSet.

    NSURL *candidatesURL = [NSURL URLWithString:[str_url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];