NSString writeToFile 带有 URL 编码字符串
我有一个 Mac 应用程序,它保留自己的日志文件。它使用 NSString 的 writeToFile 方法将信息附加到文件中。它记录的内容之一是与其交互的 Web 服务的 URL。为了对 URL 进行编码,我这样做:
searchString = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)searchString, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8 );
然后应用程序将 searchString 附加到 URL 的其余部分并将其写入日志文件。现在的问题是,添加 URL 编码行后,似乎没有任何内容写入文件。然而,该程序按预期运行。删除上面的代码行会导致所有正确信息都记录到文件中(删除该行不是一个选项,因为 searchString 必须进行 URL 编码)。
哦,我在将 NSString 写入文件时使用 NSUTF8StringEncoding。
感谢您的任何帮助。
编辑:我知道 NSString 中也有一个与 CFURLCreateStringByAddingPercentEscapes 类似的函数,但我读到它并不总是有效。如果我原来的问题无法得到解答,有人可以解释一下吗?谢谢! (编辑:使用 stringByAddingPercentEscapesUsingEncoding: 时也会出现同样的问题)
编辑 2:这是我用来将消息附加到日志文件的代码。
+(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];
}
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 );
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).
Oh and I am using NSUTF8StringEncoding when writing the NSString to the file.
Thanks for any help.
EDIT: 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! (EDIT: same problem occurs when using stringByAddingPercentEscapesUsingEncoding:
)
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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为几乎一整天后没有人提供任何答案,我将在这里发布一个疯狂的猜测:您不会意外地将要输出的字符串(其中包含百分比字符)用作 格式字符串 你是吗?
也就是说,犯了这样的错误:
NSLog(@"In format strings you can use %@ as a placeholder for an object, and %i for a plain C integer.")
而不是:
< code>NSLog(@"%@", @"在格式字符串中,您可以使用 %@ 作为对象的占位符,使用 %i 表示普通的 C 整数。");
但我会如果这被证明是问题的原因,请感到惊讶,因为它通常会导致看起来随机的输出,而不是绝对没有输出。在某些情况下,Xcode 还会给出有关它的编译器警告(当我尝试 NSLog(myString) 时,我收到“警告:格式不是字符串文字且没有格式参数”)。
因此,如果这个答案没有帮助,请不要拒绝我。如果您可以向我们展示更多您的日志代码,那么回答您的问题会更容易。至于你提供的一行,我无法发现它有什么问题。
编辑:哎呀,我有点错过你提到你正在使用
writeToFile:atomically:encoding:error:
将字符串写入文件,所以它更不可能您不小心将其视为某处的格式字符串。但我现在要保留这个答案。再说一次,你确实应该向我们展示更多你的代码...编辑:关于你对 NSString 中具有类似百分比编码功能的方法的问题,那就是
stringByAddingPercentEscapesUsingEncoding:
。当你说你听说它并不总是有效时,我不确定你在想什么样的问题。但有一件事是 CFURLCreateStringByAddingPercentEscapes 允许您指定通常不需要转义但您仍然希望转义的额外字符,而 NSString 的方法不允许您指定此字符。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?
That is, making the mistake of doing:
NSLog(@"In format strings you can use %@ as a placeholder for an object, and %i for a plain C integer.")
Instead of:
NSLog(@"%@", @"In format strings you can use %@ as a placeholder for an object, and %i for a plain C integer.");
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.
Edit: 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 ...Edit: 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 thatCFURLCreateStringByAddingPercentEscapes
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.