0

Below is the code to get an NSDictionary from Google Places AutoComplete API:

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

The code above stores the following in place.name:

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

The results appear separated by Comma. What's the best way to remove the the name of the Country before storing it in place.name. I would like to only display the address up to the State.

4

1 回答 1

1

You can use rangeOfString:options:range with the option NSBackwardsSearch to find that last comma:

    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);
于 2013-04-24T04:56:02.213 回答