【问题标题】:Data argument not used by format string but it works fine格式字符串未使用数据参数,但工作正常
【发布时间】:2013-01-01 00:13:20
【问题描述】:

我使用了 Stack Overflow 问题中的这段代码:URLWithString: returns nil:

//localisationName is a arbitrary string here
NSString* webName = [localisationName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSString* stringURL = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@,Montréal,Communauté-Urbaine-de-Montréal,Québec,Canadae&output=csv&oe=utf8&sensor=false", webName];
NSString* webStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url = [NSURL URLWithString:webStringURL];

当我将它复制到我的代码中时,没有任何问题,但是当我修改它以使用我的网址时,我遇到了这个问题:

格式字符串未使用数据参数。

但它工作正常。在我的项目中:

.h:

NSString *localisationName;

.m:

NSString* webName = [localisationName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString* stringURL = [NSString stringWithFormat:@"http://en.wikipedia.org/wiki/Hősök_tere", webName];
NSString* webStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url = [NSURL URLWithString:webStringURL];

[_webView loadRequest:[NSURLRequest requestWithURL:url]];

我该如何解决这个问题?我的代码有什么遗漏吗?

【问题讨论】:

    标签: ios uiwebview nsstring


    【解决方案1】:

    原始字符串中的@ 用作插入webName 值的占位符。在你的代码中,你没有这样的占位符,所以你告诉它把 webName 放到你的字符串中,但你没有说在哪里。

    如果您不想在字符串中插入webName,那么您的一半代码是多余的。您只需要:

    NSString* stringURL = @"http://en.wikipedia.org/wiki/Hősök_tere";
    NSString* webStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL* url = [NSURL URLWithString:webStringURL];
    
    [_webView loadRequest:[NSURLRequest requestWithURL:url]];
    

    【讨论】:

      【解决方案2】:

      +stringWithFormat: 方法将 return a string created by using a given format string as a template into which the remaining argument values are substituted。而在第一个代码块中,%@ 将被替换为 webName 的值。

      在你的修改版本中,format参数,即@"http://en.wikipedia.org/wiki/Hősök_tere",不包含任何format specifiers,所以 p>

      NSString* stringURL = [NSString stringWithFormat:@"http://en.wikipedia.org/wiki/Hősök_tere", webName];

      就这样运行(带有警告Data argument not used by format string.

      NSString* stringURL = @"http://en.wikipedia.org/wiki/Hősök_tere";

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-21
        • 2021-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-12
        • 2016-12-28
        相关资源
        最近更新 更多