我正在尝试构建一个url并确保路径中的所有特殊字符都已编码,我目前未能这样做,可能是由于误解了path
属性NSURLComponents
作品。URLPathAllowedCharacterSet不编码感叹号
NSURLComponents *components = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
NSURLQueryItem *queryItem = [NSURLQueryItem queryItemWithName: @"queryName" value: @"queryValue"];
components.queryItems = @[queryItem];
//我认为stringByAddingPercentEncodingWithAllowedCharacters将编码字符一样 “!”到 “%21”
components.path = [components.path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLPathAllowedCharacterSet]];
//返回以 “!” 一个网址,未编码的
[items addObject:components.URL];
我做得根本错误的components.path
?
感谢大家提前给予的帮助。