2012-11-21 57 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

相關問題