NSString writeToFile with URL encoded string

余生长醉 提交于 2019-12-12 19:20:02

问题


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];
}

回答1:


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 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.



来源:https://stackoverflow.com/questions/5494133/nsstring-writetofile-with-url-encoded-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!