代码之家  ›  专栏  ›  技术社区  ›  crono81

解码字符串iPhone SDK

  •  1
  • crono81  · 技术社区  · 13 年前

    我使用列表来获取ftp网站的内容,它工作得很好,但在某些情况下,我的文件名称中有特殊字符,比如这个:“Mrzo 2 2013(8).JPG”,第二个字符。当我调用列表函数y得到一个包含此数据的NSDictionary时(使用nslog):

    {
      kCFFTPResourceGroup = group;
      kCFFTPResourceLink = "";
      kCFFTPResourceModDate = "2013-03-09 19:08:00 +0000";
      kCFFTPResourceMode = 511;
      kCFFTPResourceName = "M\U00b7rzo 2 2013 (8).JPG";
      kCFFTPResourceOwner = owner;
      kCFFTPResourceSize = 145971;
      kCFFTPResourceType = 8;
    }
    

    然后使用这个

    NSLog(@"%@", [[listaArchivos objectAtIndex:0] valueForKey:(id)kCFFTPResourceName]);
    

    我明白了

    M·rzo 2 2013 (8).JPG
    

    注意第二个字符,应该是“”,我尝试了很多函数,但无法正确转换字符串,我希望有人能帮忙

    谢谢

    埃齐基尔。

    2 回复  |  直到 13 年前
        1
  •  0
  •   crono81    13 年前

    我发现了这个问题,它是在获取字典的函数上,我可以通过观看苹果公司的simpleFTPSample来修复它,在函数-(void)parseListData中的ListController.m文件中,我更改了这一行

    entryToAdd = [self entryByReencodingNameInEntry:(__bridge NSDictionary *) thisEntry encoding:NSASCIIStringEncoding];//NSUTF8StringEncoding
    

    我用ascii更改了utf8,现在我可以正确地获取文件名了。 现在的问题是无法下载该文件。

    在这一行中,来自示例的同一文件

    - (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode 
        ·
        ·
            bytesRead = [self.networkStream read:buffer maxLength:sizeof(buffer)];
            if (bytesRead < 0) {
                [self stopReceiveWithStatus:@"Network read error"];
            } else if (bytesRead == 0) {
                [self stopReceiveWithStatus:nil];
            } else {
                assert(self.listData != nil);
    
                // Append the data to our listing buffer.
    
                [self.listData appendBytes:buffer length:(NSUInteger) bytesRead];
    
                // Check the listing buffer for any complete entries and update 
                // the UI if we find any.
    
                [self parseListData];
            }
    

    我收到“Netwok读取错误”,文件流在documents文件夹中创建文件,netwokstreamdown打开,但无法读取,有什么建议吗?

    谢谢

        2
  •  0
  •   btmanikandan    13 年前

    你能试试这个代码吗

    NSString *sourcestr = @"ë,à,é,ä all to e,a,e,a";
    NSData *temp = [sourcestr dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *outputStr = [[[NSString alloc] initWithData:temp encoding:NSASCIIStringEncoding] autorelease];
    NSLog(@"converted: %@", outputStr);
    

    它对我来说很好