2017-04-13 8 views
0

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で根本的に間違って何かをやってエンコードされていない、との "!" URLを返します//?

ご協力いただき、ありがとうございます。

答えて

1

URLPathAllowedCharacterSetの可変コピーを作成し、 "!"を削除して新しいキャラクタセットを作成します。

NSMutableCharacterSet *characterSet = [[NSCharacterSet URLPathAllowedCharacterSet] mutableCopy]; 
[characterSet removeCharactersInString:@"!"]; 
1

URLPathAllowedCharacterSetが含まれてい「!」、これはあなたが使用してこのセットを列挙することができます

をコード化されることはありません。このルーチン URLPathAllowedCharacterSetが含まれているので、「!」、あなたが作成する必要があります

NSCharacterSet *set = [NSCharacterSet URLPathAllowedCharacterSet]; 

    for (int plane = 0; plane <= 16; plane++) { 
     if ([set hasMemberInPlane:plane]) { 
      UTF32Char c; 
      for (c = plane << 16; c < (plane+1) << 16; c++) { 
       if ([set longCharacterIsMember:c]) { 
        UTF32Char c1 = OSSwapHostToLittleInt32(c); // To make it byte-order safe 
        NSString *s = [[NSString alloc] initWithBytes:&c1 length:4 encoding:NSUTF32LittleEndianStringEncoding]; 
        NSLog(@"%@", s); 
       } 
      } 
     } 
    } 
関連する問題