0
NSString *Address = [placemark.addressDictionary objectForKey:@"FormattedAddressLines"];
NSLog(@"Address:%@", Address);
Address: ("Hooker Alley","San Francisco, CA 94108",USA)

我想从地址字符串中删除一些字符,例如 Hooker Alley, San Francisco, CA 94108,USA。怎么去掉这样的?请帮我

提前致谢

我试过这个:

NSString *removeCharacter = [Address stringByReplacingOccurrencesOfString:@"(" withString:@""];

但是错误出现在 First throw 调用堆栈中:

错误信息是,

-[__NSArrayM stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x81afd00 
4

5 回答 5

3

尝试这个,

NSArray *addressArray = [placemark.addressDictionary objectForKey:@"FormattedAddressLines"];
NSString *address = [addressArray componentsJoinedByString:@", "];
NSLog(@"Address:%@", address);

在您的情况下,[placemark.addressDictionary objectForKey:@"FormattedAddressLines"]返回一个数组而不是单个字符串。您可以尝试将这些数组组件连接为单个字符串,如上所示。或者,您也可以检查

id object = [placemark.addressDictionary objectForKey:@"FormattedAddressLines"];
if([object isKindOfClass[NSArray class]]) {
  //handle as above
} else if([object isKindOfClass[NSString class]]) {
  //use your code
}
于 2012-11-21T08:56:09.680 回答
1

你可以像这样删除:-

 NSString *s = @"$$$hgh$g%k&fg$$tw/-tg";
    NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"-/:;()$&@\".,?!\'[]{}#%^*+=_|~<>€£¥•."];
    s = [[s componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];

    NSLog(@"String is: %@", s);
于 2012-11-21T08:53:23.057 回答
0
    NSArray *Address=[placemark.addressDictionary objectForKey:@"FormattedAddressLines"];
     NSLog(@"Address:%@",Address);

     NSMutableString *myString = [[NSMutableString alloc]init];

    for(int i = 0 ; i < [Address count] ; i++){
    [myString appendString:[Address objectAtIndex:i]];


    if (i < [Address count]  ){
    [myString appendString:@","];
       }
 }

NSLog(@"%@",myString);
于 2012-11-21T09:00:52.597 回答
0

使用如下方式检查输出addressDictionary objectForKey提供的内容isKindOfClass

if([[placemark.addressDictionary objectForKey:@"FormattedAddressLines"] isKindOfClass[NSArray class]])
{

  NSArray *address=[placemark.addressDictionary objectForKey:@"FormattedAddressLines"];
  NSLog(@"Address:%@",Address);
  NSString *str = [address componentsJoinedByString: @","]
  NSLog(@"str:%@",str);
}
于 2012-11-21T08:56:18.047 回答
0

试试这个......它会帮助你

 NSString *unfilteredString = @"!@#$%^&*()_+|abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
NSCharacterSet *notAllowedChars = [NSCharacterSet characterSetWithCharactersInString:@"1234567890"];
NSString *resultString = [[unfilteredString componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:@""];
NSLog (@"Result: %@", resultString);
于 2012-11-21T08:56:33.507 回答