错误消息中的格式字符串未使用的数据参数

时间:2014-01-13 16:19:23

标签: ios nsstring

我一直用这行代码得到上面的警告信息,但无法弄清楚如何纠正它。

    *err = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASICompressionError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat:@"Decompression of %@ failed because we were unable to write to the destination data file at &@",sourcePath,destinationPath],
NSLocalizedDescriptionKey,
[outputStream streamError],
NSUnderlyingErrorKey,
nil]];

1 个答案:

答案 0 :(得分:1)

错误是拼写错误:“& @”而不是“%@”。

考虑更像这样编写代码:

NSString *messageText = [NSString stringWithFormat:@"Decompression of %@ failed because we were unable to write to the destination data file at %@", sourcePath, destinationPath];
NSString *streamErrorText = [outputStream streamError];
NSDictionary *userInfo = @{NSLocalizedDescriptionKey : messageText,
                           NSUnderlyingErrorKey      : streamErrorText};
NSError *err = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASICompressionError userInfo:userInfo];

使用这种代码格式,错误本身就会出现在一条线上并且很容易找到。

编写供人阅读的代码,而不是编译器。