简体   繁体   English

格式不是字符串文字,没有格式参数(不涉及NSLog)

[英]format not a string literal and no format arguments (not involving NSLog)

(I am new to objective c so apologies if this seems to be a simple question) (我是客观的新手,如果这似乎是一个简单的问题,请道歉)

I researched the following message here 我在这里研究了以下信息

format not a string literal and no format arguments 格式不是字符串文字,也不是格式参数

and most of the responses involve an NSLog statement. 并且大多数响应涉及NSLog语句。 However, my error shows up with this line 但是,我的错误出现在这一行

NSString *path = [[self applicationDocumentsDirectory] stringByAppendingFormat:[NSString stringWithFormat:@"/%@", [managedObject Name]]];

I am troubleshooting a set of code and don't seem to understand why the error is occuring here. 我正在对一组代码进行故障排除,似乎不明白为什么在这里发生错误。 any assistance on this would be appreciated. 对此有任何帮助将不胜感激。

The below should fix it. 以下应该解决它。

NSString *path = [[self applicationDocumentsDirectory] stringByAppendingFormat:[NSString stringWithFormat:@"/%@", [managedObject Name]], nil];

Alternatively 另外

NSString *path = [[self applicationDocumentsDirectory] stringByAppendingFormat:@"/%@", [managedObject Name]];

Should also do it. 也应该这样做。

You were calling two methods that expected a format parameter, you were passing one into the [NSString stringWithFormat] but not the stringByAppendingFormat method. 您正在调用两个期望格式参数的方法,您将一个传递给[NSString stringWithFormat]但不传递stringByAppendingFormat方法。

除了其他人所说的,你应该研究一下

- (NSString *)stringByAppendingPathComponent:(NSString *)aString

You are using stringByAppendingFormat , and then using stringWithFormat . 您正在使用stringByAppendingFormat ,然后使用stringWithFormat Pick one or the other. 选择一个或另一个。 Fix: 固定:

NSString *path = [[self applicationDocumentsDirectory] stringByAppendingFormat:@"/%@", [managedObject Name]];
[NSString stringWithFormat:@"/%@", [managedObject Name]

will return a string with the %@ already replaced by the value of [managedObject Name] . 将返回一个字符串,其中%@已被[managedObject Name]的值替换。 Therefore, the method stringByAppendingFormat is not getting the formatting string and any arguments. 因此,方法stringByAppendingFormat没有获取格式化字符串和任何参数。

BTW, the convention is to use method names beginning with lowercase alphabets, unlike in [managedObject Name] BTW,惯例是使用以小写字母开头的方法名称,与[managedObject Name]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM