如何从 NSURL 末尾删除 3 个字符?

发布于 2024-09-01 22:35:19 字数 2410 浏览 3 评论 0原文

我的第一个问题!我已经能够在没有寻求帮助的情况下编写这个 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]];

嘿,我没说它很漂亮。谢谢迪德里克·胡根布姆!

my first question! I've been able to code up most of this RSS reader without enlisting help (through a lot of searches through stackoverflow!) but I'm stumped here.

 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]];

The links3 array is a NSMutableArray with strings. The first few lines work flawlessly in eliminating the space at the beginning each string from that array, which is stored in 'urlbase' so they look fine when they come out. When we NSLog urlbase, we see:

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

But, when we use: [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlbase]]

We see: http://www.feedzilla.com/r/D7E6204FEDBFE541314B997AAB5D2DF9CBA2EFEE%0A

How can I fix this? Can I remove those tail elements somehow? Thanks!

Working code:

    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]];

Hey, I didn't say it was pretty. Thanks Diederik Hoogenboom!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪操控生活 2024-09-08 22:35:19

试试这个:

[urlbase stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]]

URL 字符串的最后一部分是换行符。

Try this:

[urlbase stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]]

The last part of your URL string is a line feed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文