2010-05-16 50 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