开发者

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


Beside what the others have said, you should look into

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


You are using stringByAppendingFormat, and then using 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]. Therefore, the method stringByAppendingFormat is not getting the formatting string and any arguments.

BTW, the convention is to use method names beginning with lowercase alphabets, unlike in [managedObject Name]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜