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

标签 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]];

最佳答案

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

考虑像这样编写代码:

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

使用这种代码格式,错误会单独出现在一行中并且很容易找到。

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

关于ios - 错误消息中的格式字符串未使用数据参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21096124/

相关文章:

ios - 当我们在 ios 中选择最后一个按钮时,如何检查前面的按钮是否被选中

objective-c - 这两种创建 NSString 的方式有什么区别?

ios - 字典给出不正确的值

objective-c - 为什么 [NSMutableString stringWithString : @""] work?

objective-c - 如何将数组加载到 UITableView,而不是 TableViewController

ios - 从命令行自动编辑 Google 电子表格

ios - 使用 Swift 4 在应用程序中保存/加载文件

ios - 在 Mailcore2 中使用 partID 获取 bodyHTML 时出现问题

string - 由于字符串中的 unicode 字符导致应用程序崩溃

iphone - 如何将 NSString 转换为 NSData?并且数据与字符串内容相同