I need a class to read and store directory structure. This is my first step in Objective C so I ask to you to say what you think.
Header file:
#import <Foundation/Foundation.h>
@interface FileStructureRepresentation : NSObject
- (id)initWithPath:(NSString *)path;
-(NSMutableDictionary*) parse;
@end
Code:
#import "FileStructureRepresentation.h"
@implementation FileStructureRepresentation
NSFileManager *fileManager = nil;
NSString *startPath;
- (id)initWithPath:(NSString *)path
{
self = [super init];
if (self) {
fileManager = [NSFileManager defaultManager];
startPath = path;
}
return self;
}
-(NSMutableDictionary *) parse {
return [self parseDirectory:startPath];
}
-(NSMutableDictionary *) parseDirectory:(NSString *)path {
NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
NSMutableArray *collectionOfFilesAndDirectories = [[NSMutableArray alloc] init];
NSError *error = nil;
NSArray *listOfFiles = [fileManager contentsOfDirectoryAtPath:path error:&error];
if(error.code != 0) {
NSLog(@"Error code:%d, Description:%@", error.code, error.description);
}
for(NSMutableString *s in listOfFiles) {
NSString *temporaryPath = [NSString stringWithFormat:@"%@/%@", path, s];
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:temporaryPath
error:&error];
NSString *fileType = [fileAttributes valueForKey:NSFileType];
if([fileType isEqualToString:@"NSFileTypeRegular"])
[collectionOfFilesAndDirectories addObject:s];
else
[collectionOfFilesAndDirectories addObject:[self parseDirectory:temporaryPath]];
}
[result setObject:collectionOfFilesAndDirectories forKey:path];
return result;
}
@end
Example:
FileStructureRepresentation *fileStructure = [[FileStructureRepresentation alloc] initWithPath:@"/Users/demas/temporary/tst"];
NSMutableDictionary *structureModel = [fileStructure parse];