2013-04-24 41 views
0

分隔下面的代碼是從谷歌的地方自動完成API得到一個NSDictionary:的iOS:使用JSON解析逗號

(GooglePlacesAutocompletePlace *)placeFromDictionary:(NSDictionary *)placeDictionary 
{ 
    GooglePlacesAutocompletePlace *place = [[self alloc] init]; 
    place.name = [placeDictionary objectForKey:@"description"]; 
    return place; 
} 

上面存儲瞭如下的place.name代碼:

"Name: Apple Store Palo Alto, University Avenue, Palo Alto, CA, United States" 

的結果顯示由逗號分隔。在將它存儲在place.name之前,刪除國家名稱的最佳方法是什麼?我只想顯示地址直到國家。

回答

1

可以使用rangeOfString:選項:範圍的選項NSBackwardsSearch找到最後一個逗號:

NSString *s = @"Name: Apple Store Palo Alto, University Avenue, Palo Alto, CA, United States"; 
    NSRange lastCommaRange = [s rangeOfString:@"," options:NSBackwardsSearch range:NSMakeRange(0, s.length)]; 
    NSString *noCountryString = [s substringToIndex:lastCommaRange.location]; 
    NSLog(@"%@",noCountryString);