URLWithString: returns nil

后端 未结 8 1809
遥遥无期
遥遥无期 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: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

提交回复
热议问题