我正在尝试从使用REST查询下载的数据中的字符串中删除子字符串,我使用“stringByReplacingOccurrencesOfString”将@“Point”替换为@“”并将其存储在temp变量中,但原始变量并且temp变量没有更新。
-(void)fetchURL:(NSURL *)url
{
url = [NSURL URLWithString:@"http://team41.kmiller.io/api/getByLocation/Verizon%20Wireless/100/33.7/-84.0"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response,
NSData *data, NSError *connectionError)
{
if (data.length > 0 && connectionError == nil)
{
NSDictionary *greeting = [NSJSONSerialization JSONObjectWithData:data
options:0
error:NULL];
if ([[greeting objectForKey:@"result"] isEqualToString:@"success"])
{
NSString * temp;
for(NSDictionary* key in [greeting objectForKey:@"json"])
{
NSString * strength = [key objectForKey:@"dbm_strength"];
NSString * location = [key objectForKey:@"astext(location)"];
temp = [location stringByReplacingOccurrencesOfString:@"Point" withString:@""];
NSLog(@"%@",location);
NSLog(@"%@",temp);
break;
}
// TODO: Parse location and strength
// TODO: Update the records to database
}
}
}];
}
答案 0 :(得分:0)
REST响应为POINT
,您尝试替换Point
实际回复
"POINT(33.78272 -84.40426)"
答案 1 :(得分:0)
虽然Vig的答案是正确的并指出一个问题,但更糟糕的是:subStringByReplacingOccurrencesOfString:返回一个新的NSString *。它不会修改消息的接收者。修改后的字符串存储在temp中,传递给NSLog,然后丢弃。此代码不会将结果存储在任何位置。