Question

I would like to merge the two directories listings (already done and works they show up in NSTableView), but also display the contents of the files in an NSScrollview, now the problem lies in iterating through the list, and I couldn't figure out how I would come about that problem, I tried different techniques.

For now I get: "-[NSTextView replaceCharactersInRange:withString:]: nil NSString given.", probably because the iteration code is incorrect...

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];
        }
    }
}
Was it helpful?

Solution

You don't need to iterate. All you need to do is to check which array the file name came from.

Assuming you aren't sorting the list, this is if (row >= directoryList1.count). This check tells you which list it came from so you can set the prefix.

If you are sorting and the names are unique you could use [directoryList1 containsObject:...).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top