简体   繁体   English

如何在iOS5中替换此语句

[英]How can i replace this statement in ios5

When i write ' stringWithContentsOfURL' i am getting error that 'stringWithContentsOfURL is Deprecated'. 当我写“ stringWithContentsOfURL”时,我收到“不赞成使用stringWithContentsOfURL”的错误提示。

    -(CLLocationCoordinate2D) addressLocation 
{
        NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 
      [addressField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
        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];
        }
        else {
            //Show error
        }
        CLLocationCoordinate2D location;
        location.latitude = latitude;
        location.longitude = longitude;

        return location;
    }

How can i replace this line. 我该如何替换这条线。 Thanks! 谢谢!

NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];

It has been replaced with 它已被替换为

stringWithContentsOfURL:encoding:error: 

or 要么

stringWithContentsOfURL:usedEncoding:error:

Example code 范例程式码

NSError* error = nil;
NSString* locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];

Check the documentation for stringWithContentsOfURL. 检查文档中的stringWithContentsOfURL。 In the NSString documentation it tells you exactly what methods are used to replace that deprecated one, and its good to know how to do as checking the docs for this sort of stuff is something you'll do frequently. 在NSString文档中,它准确地告诉您使用什么方法来替换不推荐使用的方法,并且很高兴知道如何做,因为您经常会检查此类内容的文档。

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

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