无效的转化说明符'+'

时间:2014-04-26 01:53:48

标签: objective-c ios7 nsstring

我已经查了过来,但答案似乎总是特定于这个问题。

我尝试使用JSON从Web服务器请求信息,并将信息显示在表视图中。给我带来麻烦的代码如下:

CalendarCommunicator.m

#import "CalendarCommunicator.h"
#import "CalendarCommDelegate.h"


@implementation CalendarCommunicator

- (void)searchGroupsAtCoordinate:(CLLocationCoordinate2D)coordinate
{
    NSString *urlAsString = [NSString stringWithFormat:@"http://medpak.costarica.com/api/content/render/false/type/json/query/+structureName:calendarEvent%20+(conhost:9fe93d82-1cd8-46b1-9dda-8b940e407d23%20fonhost:SYSTEM_HOST)%20+languageId:1", coordinate.latitude, coordinate.longitude];
    NSURL *url = [[NSURL alloc] initWithString:urlAsString];
    NSLog(@"%@", urlAsString);

    [NSURLConnection sendAsynchronousRequest:[[NSURLRequest alloc] initWithURL:url] queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

        if (error) {
            [self.delegate fetchingGroupsFailedwithError:error];
        } else {
            [self.delegate recievedGroupsJSON:data];
        }
    }];
}

@end

NSString *urlAsString部分,我收到了网址警告。它说'""'''''''这到底是什么意思?有很多' +'在URL中签名或我遗失了什么?

1 个答案:

答案 0 :(得分:8)

原因是%对URL的百分比编码和格式化字符串都有意义。在这种情况下,+stringWithFormat会在您的字符串中看到%20+,并认为您正在为其提供格式化命令。对于您的网址格式字符串中的文字%,请将它们设为%%,即将%%传递给字符串格式化方法/函数以获取文字%

但是,在您的特定情况下,我不确定您要尝试替换哪些位置以及哪些是您想要的百分比逃脱。

相关问题