【问题标题】:Remove last portion of the NSURL: iOS删除 NSURL 的最后一部分:iOS
【发布时间】:2013-05-05 19:59:16
【问题描述】:

我试图只删除 url 的最后一部分,它是一个 FTP URL。

假设,我有一个类似的 URL:> ftp://ftp.abc.com/public_html/somefolder/。删除最后一部分后,我应该将其命名为:ftp://ftp.abc.com/public_html/

我尝试过使用stringByDeletingLastPathComponenetURLByDeletingLastPathComponent,但它们没有正确删除最后一部分。它们改变了 url 的整体外观。

例如,使用上述方法后,这是我得到的 URL 格式 ftp:/ftp.abc.com/public_html/。它删除了“ftp://”中的一个“/”,这使我的程序崩溃。

如何在不影响 URL 其余部分的情况下只删除最后一部分?

更新:

NSURL * stringUrl = [NSURL URLWithString:string];
NSURL * urlByRemovingLastComponent = [stringUrl URLByDeletingLastPathComponent];
NSLog(@"%@", urlByRemovingLastComponent);

使用上面的代码,我得到的输出是:- ftp:/ftp.abc.com/public_html/

【问题讨论】:

  • -stringByDeletingLastPathComponent 确实会压缩任何双斜杠。它的文档确实明确指出它仅适用于路径,而不是 URL!
  • -URLByDeletingLastPathComponent 可以正常工作。对于您的上述代码示例,我唯一能看到的输出ftp:/ftp.abc.com/public_html/ 是输入是否与ftp:/ftp.abc.com/public_html/somefolder/ 类似的格式错误@
  • 快速跟进:结果在 OS X 10.6(可能是 iOS 4)-URLByDeletingLastPathComponent 错误处理路径部分中的双斜杠。试图删除这样的组件会导致 ../ 被附加
  • 一个随机的想法:是否有可能您从文本字段输入的 URL 实际上包含一个尾随换行符?事实证明,在 OS X 10.9 和 iOS 7 之前,最后一个字符可能是无效字符。我想这可能会搞砸路径编辑逻辑

标签: iphone ios6 ftp nsstring nsurl


【解决方案1】:

嗯。鉴于上述输入,URLByDeletingLastPathComponent 可以完美运行。

NSURL *url = [NSURL URLWithString:@"ftp://ftp.abc.com/public_html/somefolder/"];
NSLog(@"%@", [url URLByDeletingLastPathComponent]);

返回

ftp://ftp.abc.com/public_html/

您是否有一些示例代码会产生不正确的结果?

最大

【讨论】:

  • @Max:请检查我的问题中的更新。
  • 很遗憾,我无法复制您遇到的错误。会不会是输入的字符串格式错误?
  • @Max:我不确定,但是我提供来自 TextFields 的输入。
  • 当你 NSLog(@"%@", string);就在尝试删除最后一个路径组件之前?
  • 它确实给出了正确的 URL 格式。这是我通过文本字段输入的字符串
【解决方案2】:

现在试试

    NSString* filePath = @"ftp://ftp.abc.com/public_html/somefolder/.";

    NSArray* pathComponents = [filePath pathComponents];
    NSLog(@"\n\npath=%@",pathComponents);
    if ([pathComponents count] > 2) {
            NSArray* lastTwoArray = [pathComponents subarrayWithRange:NSMakeRange([pathComponents count]-2,2)];
            NSString* lastTwoPath = [NSString pathWithComponents:lastTwoArray];
             NSLog(@"\n\nlastTwoArray=%@",lastTwoPath);

            NSArray *listItems = [filePath componentsSeparatedByString:lastTwoPath];
            NSLog(@"\n\nlist item 0=%@",[listItems objectAtIndex:0]);

     }


output

path=(
      "ftp:",
      "ftp.abc.com",
      "public_html",
      somefolder,
      "."
      )

lastTwoArray =somefolder/.

list item 0 =ftp://ftp.abc.com/public_html/

【讨论】:

  • 这确实有效,但是 URL 可以是任何东西,如果是 ftp.abc.com/public_html/somefolder/someOtherFolder 怎么办?这种方法不会产生正确的结果。
  • ohhh.. 我认为 url 是固定的。等等
  • 谢谢萨米尔。尽管您的回答没有解决我的问题,但我不得不找到其他解决方案。但是,仍然......我将您的解决方案用于其他目的。
【解决方案3】:

如何提取 NSURL 的最后一部分的示例。在这种情况下,文件的位置。 Sqlite核心数据

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreAPI.sqlite"];

NSString *localPath = [storeURL absoluteString];

NSArray* pathComponents = [localPath pathComponents];

NSLog(@"%@",[pathComponents objectAtIndex:6]);

NSString * nombre = [NSString stringWithFormat:@"%@", [pathComponents objectAtIndex:6]];

此代码返回文件 CoreAPI.sqlite 的名称

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-15
    • 2010-11-03
    • 1970-01-01
    • 2014-12-30
    • 2018-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多