URLWithString: returns nil

后端 未结 8 1811
遥遥无期
遥遥无期 2020-12-02 13:12

it may be very easy, but I don\'t seems to find out why is URLWithString: returning nil here.

//localisationName is a arbitrary string here
NSSt         


        
相关标签:
8条回答
  • 2020-12-02 13:32

    I think your accented characters are throwing things off; they won't be handled by -stringByAddingPercentEscapesUsingEncoding:.

    0 讨论(0)
  • 2020-12-02 13:33

    UPDATE: Since stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding is deprecated now, you should use stringByAddingPercentEncodingWithAllowedCharacters

    [stringURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    

    Here's the list of allowed characters set depending on your case.

    + (NSCharacterSet *)URLUserAllowedCharacterSet;
    + (NSCharacterSet *)URLPasswordAllowedCharacterSet;
    + (NSCharacterSet *)URLHostAllowedCharacterSet;
    + (NSCharacterSet *)URLPathAllowedCharacterSet;
    + (NSCharacterSet *)URLQueryAllowedCharacterSet;
    + (NSCharacterSet *)URLFragmentAllowedCharacterSet;
    

    Reference: https://developer.apple.com/reference/foundation/nsstring/1411946-stringbyaddingpercentencodingwit

    0 讨论(0)
提交回复
热议问题