如何获取 Cocoa/Obj-C 中两个或多个文件的共同祖先目录?

发布于 2024-09-01 22:58:46 字数 83 浏览 14 评论 0原文

我有一堆文件,要么是 NSString 要么是 NSURL(没关系;它们在大多数情况下是可以互换的),我需要一种方法找到共同祖先目录。有谁知道该怎么做?

I have a bunch of files, either NSStrings or NSURLs (it doesn't matter; they are, for the most part, interchangeable), and I need a way to find the common ancestor directory. Does anyone know how to do this?

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

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

发布评论

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

评论(3

故人如初 2024-09-08 22:58:46

我可以发誓某处有一个 pathByJoiningPathComponents 方法,或者至少有一个类似的方法,但我一定在想别的东西。这对路径有用,如果你使用的是 10.6,它也可能适用于 URL(我只用路径测试过):

NSString *path1 = @"/path/to/file1.txt";
NSString *path2 = @"/path/to/file/number2.txt";

NSArray *path1Comps = [path1 pathComponents];
NSArray *path2Comps = [path2 pathComponents];

NSUInteger total = [path1Comps count];
if ([path2Comps count] < total)
    total = [path2Comps count];  // get the smaller of the two

NSUInteger i;
for (i = 0; i < total; i++)
    if (![[path1Comps objectAtIndex:i] isEqualToString:[path2Comps objectAtIndex:i]])
        break;

NSArray *commonComps = [path1Comps subarrayWithRange:NSMakeRange(0, i)];

// join commonComps together to get the common path as a string

不幸的是,我认为没有“内置”方法可以做到这一点。

如果您有一个要查找共同祖先的文件路径数组,您可以执行以下操作:

NSArray *allPaths = [NSArray arrayWithObjects:@"/path/to/1.txt", @"/path/to/number/2.txt", @"/path/to/number/3/file.txt", nil];

// put some checks here to make sure there are enough paths in the array.

NSArray *currentCommonComps = [[allPaths objectAtIndex:0] pathComponents];

for (NSUInteger i = 1; i < [allPaths count]; i++)
{
    NSArray *thisPathComps = [[allPaths objectAtIndex:i] pathComponents];
    NSUInteger total = [currentCommonComps count];
    if ([thisPathComps count] < total)
        total = [thisPathComps count];

    NSUInteger j;
    for (j = 0; j < total; j++)
        if (![[currentCommonComps objectAtIndex:j] isEqualToString:[thisPathComps objectAtIndex:j]])
            break;

    if (j < [currentCommonComps count])
        currentCommonComps = [currentCommonComps subarrayWithRange:NSMakeRange(0, j)];

    if ([currentCommonComps count] == 0)
        break; // no point going on
}

// join currentCommonComps together

如果您想保持自动释放池干净,您可能需要显式分配和释放其中一些对象,特别是如果您有一大堆路径。

I could have sworn there was a pathByJoiningPathComponents method somewhere, or at least one like it, but I must be thinking of something else. This does the trick for paths, it may work for URLs too if you're on 10.6 (I only tested it with paths):

NSString *path1 = @"/path/to/file1.txt";
NSString *path2 = @"/path/to/file/number2.txt";

NSArray *path1Comps = [path1 pathComponents];
NSArray *path2Comps = [path2 pathComponents];

NSUInteger total = [path1Comps count];
if ([path2Comps count] < total)
    total = [path2Comps count];  // get the smaller of the two

NSUInteger i;
for (i = 0; i < total; i++)
    if (![[path1Comps objectAtIndex:i] isEqualToString:[path2Comps objectAtIndex:i]])
        break;

NSArray *commonComps = [path1Comps subarrayWithRange:NSMakeRange(0, i)];

// join commonComps together to get the common path as a string

I don't think there is a “built-in” way to do it, unfortunately.

If you have an array of file paths that you want to find the common ancestor, you could do something like this:

NSArray *allPaths = [NSArray arrayWithObjects:@"/path/to/1.txt", @"/path/to/number/2.txt", @"/path/to/number/3/file.txt", nil];

// put some checks here to make sure there are enough paths in the array.

NSArray *currentCommonComps = [[allPaths objectAtIndex:0] pathComponents];

for (NSUInteger i = 1; i < [allPaths count]; i++)
{
    NSArray *thisPathComps = [[allPaths objectAtIndex:i] pathComponents];
    NSUInteger total = [currentCommonComps count];
    if ([thisPathComps count] < total)
        total = [thisPathComps count];

    NSUInteger j;
    for (j = 0; j < total; j++)
        if (![[currentCommonComps objectAtIndex:j] isEqualToString:[thisPathComps objectAtIndex:j]])
            break;

    if (j < [currentCommonComps count])
        currentCommonComps = [currentCommonComps subarrayWithRange:NSMakeRange(0, j)];

    if ([currentCommonComps count] == 0)
        break; // no point going on
}

// join currentCommonComps together

You may want to explicitly allocate and release some of these objects if you want to keep the autorelease pool clean, especially if you have a large array of paths.

巴黎盛开的樱花 2024-09-08 22:58:46

将路径表示为组件的 NSArray。 (在 Mac OS X 10.6 及更高版本中,向每个对象发送 pathComponents 消息;在早期版本和 iPhone 操作系统中,您需要向 NSURL 对象发送 path 消息获取 NSStrings,然后发送 pathComponents 消息。)

拥有一个包含到目前为止公共路径的 NSMutableArray。将其初始化为第一个路径的组件。

对于每个后续路径,使用 NSEnumerator 同步迭代该路径和当前路径。

  • 如果公共路径到目前为止用完,则不做任何改变。
  • 如果您正在检查的路径用完,则它是新的公共路径。
  • 如果遇到不相等的组件,则它之前的所有组件都是新的公共路径。在这里打破锁步迭代。

完成后,您将拥有零个或多个路径组件的数组。将这些连接到绝对路径字符串中将生成公共路径字符串。

Represent the paths as NSArrays of components. (In Mac OS X 10.6 and later, send each object a pathComponents message; in earlier versions and on the iPhone OS, you'll need to send NSURL objects path messages to get NSStrings, then send those the pathComponents messages.)

Have an NSMutableArray containing the common path so far. Initialize it to the first path's components.

For each subsequent path, iterate both that path and the current path so far in lockstep using NSEnumerators.

  • If the common path so far runs out, no change.
  • If the path you're examining runs out, it is the new common path.
  • If you encounter an inequal component, all components before it are the new common path. Break the lockstep iteration here.

When you finish, you'll have an array of zero or more path components. Joining these into an absolute path string will produce the common path string.

溇涏 2024-09-08 22:58:46

获取一个文件,获取下一个文件,迭代其祖先(NSString 的 pathComponents 方法对此很有用),直到找到它们的共同点。然后转到下一个文件,看看它是否具有相同的祖先。如果没有,请继续回溯,直到找到他们的共同点。不断重复此操作,直到到达列表的末尾。

Take a file, take the next file, iterate through its ancestors (NSString's pathComponents method will be useful for this) until you find one that they have in common. Then move onto the next file, see if it has the same ancestor. If not, keep going back until you find one they do have in common. Keep repeating this until you reach the end of the list.

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