繁体   English   中英

通过此代码链接获得警告

[英]getting warning with this link of code

 -(CLLocationCoordinate2D) addressLocation {

    NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 
   [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
    NSLog(@"locationString %@",locationString);
    NSArray *listItems = [locationString componentsSeparatedByString:@","];

    double latitude = 0.0;
    double longitude = 0.0;

    if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
        latitude = [[listItems objectAtIndex:2] doubleValue];
        longitude = [[listItems objectAtIndex:3] doubleValue];
        NSLog(@"listItems %@",[listItems objectAtIndex:2]);
    }
    else {
        //Show error
    }
    CLLocationCoordinate2D location;
    location.latitude = latitude;
    location.longitude = longitude;

    return location;
}

不推荐使用StringWithContentsOfURL。...

如果让您感到困惑的是,此“不推荐使用StringWithContentsOfURL”。 在Apple文档中的快速搜索显示:

stringWithContentsOfURL :返回一个字符串,该字符串是通过从给定URL命名的文件中读取数据而创建的。 在iOS 2.0中已弃用改为使用stringWithContentsOfURL:encoding:error:stringWithContentsOfURL:usedEncoding:error:。

一个例子是:

stringWithContentsOfURL:yourURL encoding:NSUTF8StringEncoding error:NULL

尝试而不是: NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];

NSData *locationData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
NSString *locationString = [[NSString alloc] initWithData:locationData encoding:NSASCIIStringEncoding];

这种方式的好处是,您可以改用NSURLConnection来获取数据(异步)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM