且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

NSString writeToFile,带有URL编码字符串

更新时间:2023-02-23 12:45:57

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

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.