Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 使用此代码链接获取警告_Iphone - Fatal编程技术网

Iphone 使用此代码链接获取警告

Iphone 使用此代码链接获取警告,iphone,Iphone,StringWithContentsOfURL已弃用 如果让您感到困惑的是“StringWithContentsOfURL不推荐使用”。在苹果的文档中快速搜索显示: stringWithContentsOfURL: 返回通过从给定URL命名的文件中读取数据而创建的字符串。(在iOS 2.0中不推荐使用。请改用stringWithContentsOfURL:encoding:error:或stringWithContentsOfURL:usedEncoding:error:) 例如: -(CLL

StringWithContentsOfURL已弃用

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

stringWithContentsOfURL: 返回通过从给定URL命名的文件中读取数据而创建的字符串。(在iOS 2.0中不推荐使用。请改用stringWithContentsOfURL:encoding:error:stringWithContentsOfURL:usedEncoding:error:

例如:

 -(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: 返回通过从给定URL命名的文件中读取数据而创建的字符串。(在iOS 2.0中不推荐使用。请改用stringWithContentsOfURL:encoding:error:stringWithContentsOfURL:usedEncoding:error:

例如:

 -(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;
}
尝试而不是:
NSString*locationString=[NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]]

stringWithContentsOfURL:yourURL encoding:NSUTF8StringEncoding error:NULL
这种方式的好处是,您可以使用NSURLConnection(异步)来获取数据。

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

stringWithContentsOfURL:yourURL encoding:NSUTF8StringEncoding error:NULL
这种方式的好处是可以使用NSURLConnection(异步)获取数据