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

我想删除从地址字符串像胡克巷子,旧金山,CA 94108,USA一些字符。 如何移除?请帮我如何在iPhone中从NSString中删除某些字符?

由于提前

我尝试这样做:

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

但错误出现在第一掷调用堆栈:

错误消息,

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

什么是确切的错误?请尝试使用“地址”而不是“地址”作为变量名称。它与类名和数据类型名称相混淆。 – iDev

+1

- [__ NSArrayM stringByReplacingOccurrencesOfString:withString:]:无法识别的选择器发送到实例0x81afd00 – SampathKumar

+0

您的地址变量返回一个数组 –

回答

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 
} 
+0

非常感谢你的帮助 – SampathKumar

+1

它完美的作品 – SampathKumar

1

您可以删除,如: -

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

    NSLog(@"String is: %@", s); 
+0

感谢您的回复 – SampathKumar

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); 
} 
0

一次试试这个....这将帮助你

NSString *unfilteredString = @"[email protected]#$%^&*()_+|abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; 
NSCharacterSet *notAllowedChars = [NSCharacterSet characterSetWithCharactersInString:@"1234567890"]; 
NSString *resultString = [[unfilteredString componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:@""]; 
NSLog (@"Result: %@", resultString); 
+0

这不会产生所需的输出。同样的答案总是不起作用。这将工作这个问题http://stackoverflow.com/questions/13487931/issue-with-replacing-special-characters/13488065#13488065 –

+0

我发布这个答案,因为在问题Minu说,“地址是一个字符串”.. 。如果它是一个字符串,它将起作用。 – Murali

+0

没关系。但是不允许的字符列表中的()在哪里?她想删除(),而不是数字 –

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); 
+0

为什么[推倒重来(http://en.wikipedia.org/wiki/Reinventing_the_wheel)?为什么不使用'[Address componentsJoinedByString:@“,”]'? – iDev

相关问题