NSString writeToFile,带有URL编码字符串 [英] NSString writeToFile with URL encoded string

查看:166
本文介绍了NSString writeToFile,带有URL编码字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Mac应用程序,它保留自己的日志文件。它使用NSString的writeToFile方法将信息附加到文件。它记录的事情之一是它正在与之交互的Web服务的URL。要编码的URL,我这样做:

I have a Mac application that keeps it's own log file. It appends info to the file using NSString's writeToFile method. One of the things that it logs are URL's of web services that it is interacting with. To encode the URL, I'm doing this:

searchString = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)searchString, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8 );

然后应用程序将searchString附加到URL的其余部分,并将其写入日志文件。现在的问题是,在添加该URL编码行后,似乎没有写入到该文件。然而,程序运行正常。删除上面的代码行会导致所有正确的信息被记录到文件中(删除该行不是一个选项,因为searchString必须是URL编码的)。

The app then appends searchString to the rest of the URL and writes it to the log file. Now the problem is that after adding that URL encoding line, nothing seems to be getting written to the file. The program functions as expected otherwise however. Removing the line of code above results in all of the correct information being logged to the file (removing that line is not an option because searchString must be URL encoded).

在编写NSString到文件时我使用NSUTF8StringEncoding。

Oh and I am using NSUTF8StringEncoding when writing the NSString to the file.

感谢任何帮助。

我知道在NSString中也有一个类似的函数CFURLCreateStringByAddingPercentEscapes,但我已经读过,它并不总是工作。如果我的原始问题不能得到回答,任何人都可以谈谈这个问题吗?谢谢! (编辑:使用 stringByAddingPercentEscapesUsingEncoding:时会出现同样的问题)

I know there's also a similar function to CFURLCreateStringByAddingPercentEscapes in NSString, but I've read that it doesn't always work. Can anyone shed some light on this if my original question cannot be answered? Thanks! ( same problem occurs when using stringByAddingPercentEscapesUsingEncoding:)

编辑2:这是我使用的代码

EDIT 2: Here's the code that I'm using to append messages to the log file.

+(void)logText:(NSString *)theString{
    NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,NSUserDomainMask,YES) objectAtIndex:0];
    NSString *path = [docsDirectory stringByAppendingPathComponent:@"Folder/File.log"];
    NSString *fileContents = [[[NSString alloc] initWithContentsOfFile:path] autorelease];
    if([fileContents lengthOfBytesUsingEncoding:NSUTF8StringEncoding] >= 204800){
        fileContents = @"";
    }
    NSString *timeStamp = [[NSDate date] description];
    timeStamp = [timeStamp stringByAppendingString:@": "];
    timeStamp = [timeStamp stringByAppendingString:theString];
    fileContents = [fileContents stringByAppendingString:timeStamp];
    fileContents = [fileContents stringByAppendingString:@"\n"];
    [fileContents writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:NULL];
}


推荐答案

没有人提供任何答案,我将在这里发布一个野生猜测:你不是不小心使用你想要输出的字符串(百分之一英寸)作为一个格式字符串

Because after almost a whole day no one else has offered any answers, I'm going to post a wild guess here: you're not accidentally using the string you want to output (with percent characters in it) as a format string are you?

NSLog(@)

That is, making the mistake of doing:

在格式字符串中,您可以使用%@作为对象的占位符,%i用于纯C整数。)

NSLog(@"In format strings you can use %@ as a placeholder for an object, and %i for a plain C integer.")

of:

NSLog(@%@,@在格式字符串中,可以使用%@作为对象的占位符,我为一个简单的C整数。);

但我会惊讶,如果这是原因,问题,因为它通常导致随机的输出,而不是绝对没有输出。在某些情况下,Xcode也给编译器警告(当我尝试 NSLog(myString),我得到警告:格式不是字符串字面量,没有格式参数) 。

But I'm going to be surprised if this turns out to be the cause of your problem, as it usually causes random-looking output, rather than absolutely no output. And in some cases, Xcode also gives compiler warnings about it (when I tried NSLog(myString), I got "warning: format not a string literal and no format arguments").

所以,如果这个答案没有帮助,不要拍我。如果您可以向我们展示更多的日志记录代码,则更容易回答您的问题。至于您提供的一行,我无法检测到它的任何错误。

So don't shoot me down if this answer doesn't help. It would be easier to answer your question if you could show us more of your logging code. As for the one line you provided, I can't detect anything wrong with it.

编辑:糟糕,我错过了上面提到你使用 writeToFile:atomically:encoding:error:将字符串写入文件,因此你不可能不小心把它当作格式字符串。但我现在要离开这个答案。再次,你应该真的给我们更多的代码,虽然...

Oops, I kind of missed that you mentioned you're using writeToFile:atomically:encoding:error: to write the string to the file, so it's even more unlikely you're accidentally treating it as a format string somewhere. But I'm going to leave this answer up for now. Again, you should really show us more of your code though ...

编辑:关于你的问题NSString中的方法有类似%编码功能,这将是 stringByAddingPercentEscapesUsingEncoding:。我不知道你在想什么样的问题,当你说你听说它并不总是工作。但是有一件事是, CFURLCreateStringByAddingPercentEscapes 允许你指定通常不需要转义,但是你仍然想要转义的额外的字符,而NSString的方法不' t允许您指定此。

Regarding your question on a method in NSString that has similar percent encoding functionality, that would be stringByAddingPercentEscapesUsingEncoding:. I'm not sure what kind of problems you're thinking of when you say you've heard it doesn't always work. But one thing is that CFURLCreateStringByAddingPercentEscapes allows you to specify extra characters that don't normally have to be escaped but which you still want to be escaped, while the method of NSString doesn't allow you to specify this.

这篇关于NSString writeToFile,带有URL编码字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆