【发布时间】:2011-02-20 04:34:18
【问题描述】:
我的第一个问题!我已经能够在不寻求帮助的情况下编写大部分这个 RSS 阅读器(通过通过 stackoverflow 进行大量搜索!)但我在这里被难住了。
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!
【问题讨论】:
-
%0A 是换行符(行尾)。它可能已经在 urlbase 中,您只是在 NSLog 中看不到它,因为它是空格。看看你是怎么把它放在那里的。
标签: iphone objective-c ipod-touch