2010-05-16 82 views
0

我的第一個問題!我已經能夠編寫大部分這個RSS閱讀器而無需獲得幫助(通過大量的搜索,通過計算器!),但我在這裏難倒了。如何從NSURL的末尾刪除3個字符?

NSString *urlbase = [[NSString alloc] initWithFormat:[links3 objectAtIndex:indexPath.row]]; 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlbase]]; 
NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet]; 
NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"]; 

NSArray *parts = [urlbase componentsSeparatedByCharactersInSet:whitespaces]; 
NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings]; 
urlbase = [filteredArray componentsJoinedByString:@" "]; 

NSLog(@"%@ %i" , urlbase, 4353); 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlbase]]; 

links3數組是帶字符串的NSMutableArray。前幾行完美地消除了從數組開始的每個字符串的空間,這些字符串存儲在'urlbase'中,因此當它們出來時它們看起來很好。當我們的NSLog urlbase,我們看到:

http://www.feedzilla.com/r/D7E6204FEDBFE541314B997AAB5D2DF9CBA2EFEE 

但是,當我們使用:[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlbase]]

我們看到:http://www.feedzilla.com/r/D7E6204FEDBFE541314B997AAB5D2DF9CBA2EFEE%0A

我該如何解決這個問題?我能否以某種方式移除這些尾巴元素?謝謝!

工作代碼:

NSMutableArray *links3 = [[NSMutableArray alloc] init]; 
RSSAppDelegate *appDelegate = (RSSAppDelegate *)[[UIApplication sharedApplication] delegate]; 
links3 = appDelegate.links; 
NSLog(@"%@ %i", [links3 objectAtIndex:indexPath.row], 3453); 
NSString *urlbase = [[NSString alloc] initWithFormat:[links3 objectAtIndex:indexPath.row]]; 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlbase]]; 
NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet]; 
NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"]; 

NSArray *parts = [urlbase componentsSeparatedByCharactersInSet:whitespaces]; 
NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings]; 
urlbase = [filteredArray componentsJoinedByString:@" "]; 

NSLog(@"%@ %i" , urlbase, 4353); 


NSLog(@"%@ %i" , urlbase, 345346); 
NSString* fixed = [urlbase stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]]; 

NSURL *hellothere = [NSURL URLWithString:fixed]; 
NSLog(@"%@ %i", hellothere, 54); 


    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:fixed]]; 

嘿,我並沒有說這是漂亮。感謝Diederik Hoogenboom!

+1

%0A是一個換行字符(行尾)。它可能已經在urlbase中,你只是沒有在NSLog中看到它,因爲它是空白的。首先看看你是如何得到它的。 – progrmr 2010-05-16 21:43:06

回答

1

試試這個:

[urlbase stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]] 

你的URL字符串的最後一部分是換行。

+0

非常感謝!這很好。 – saywhatman 2010-05-16 21:21:21

+0

好聽!你能否將這個答案標記爲解決它的問題,謝謝。 – diederikh 2010-05-21 18:51:11