显示两个合并目录列表的内容

时间:2014-01-17 17:32:50

标签: objective-c nsarray nsscrollview

我想合并两个目录列表(已完成并在NSTableView中显示它们的工作),但也在NSScrollview中显示文件的内容,现在问题在于迭代列表,我无法'弄清楚我将如何解决这个问题,我尝试了不同的技术。

现在我得到:“ - [NSTextView replaceCharactersInRange:withString:]:nil给NSString。”,可能是因为迭代代码不正确......

NSInteger row = [logsTableView selectedRow];

NSString *path1 = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Logs/"];
NSString *path2 = @"/Library/Logs/";

NSArray *directoryList1 = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:path1 error:nil]
                           pathsMatchingExtensions:[NSArray arrayWithObjects:@"log", nil]];

NSArray *directoryList2 = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:path2 error:nil]
                           pathsMatchingExtensions:[NSArray arrayWithObjects:@"log", nil]];

NSMutableArray *directoryList = [NSMutableArray array];
[directoryList addObjectsFromArray:directoryList1];
[directoryList addObjectsFromArray:directoryList2];

for (NSUInteger i = 0; i < directoryList.count; i++)
{        
    if (row == i)
    {
        for (NSUInteger i = 0; i < directoryList1.count; i++)
        {
            NSString *filePath = [NSHomeDirectory() stringByAppendingFormat:@"Library/Logs/%@", [directoryList objectAtIndex:i]];

            NSString *content = [NSString stringWithContentsOfFile:filePath
                                                      encoding:NSUTF8StringEncoding
                                                         error:NULL];

            [logsScrollViewTextView setString:content];
        }
        for (NSUInteger i = directoryList.count - directoryList1.count; i < directoryList.count; i++)
        {
            NSString *filePath = @[@"/Library/Logs/%@", [directoryList objectAtIndex:i]];

            NSString *content = [NSString stringWithContentsOfFile:filePath
                                                          encoding:NSUTF8StringEncoding
                                                             error:NULL];

            [logsScrollViewTextView setString:content];
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您不需要迭代。您需要做的就是检查文件名来自哪个数组。

假设您没有对列表进行排序,则为if (row >= directoryList1.count)。此检查会告诉您它来自哪个列表,以便您可以设置前缀。

如果您要排序且名称是唯一的,则可以使用[directoryList1 containsObject:...)