我正在嘗試構建一個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
?
感謝大家提前給予的幫助。