Chandan
Chandan

Reputation: 404

Creating file and directory in iOS

I am developing an iOS app using Xcode. In one of the interfaces implementation (say interface A) I am creating file under documents directory. I am able to access it from interface A. However from another interface(Say interface B) if I try to access the file I have created in interface A I get file not found. The method I have used in interface B to access the contents of file is given below

- (void)loadData{

if ([[NSFileManager defaultManager] fileExistsAtPath:_appFile]) {
    NSLog(@"Directory Exists");
    _appFile = [_appFile stringByAppendingPathComponent:@"info.txt"];
    if ([[NSFileManager defaultManager] fileExistsAtPath:_appFile]) {

        NSLog(@"File Exists");
        NSData *data = [[NSData alloc] initWithContentsOfFile:_appFile];

        if (data!=nil) {
            NSMutableDictionary *dict = (NSMutableDictionary *)
            [NSKeyedUnarchiver unarchiveObjectWithData:data];
            ToDoItem *item = [dict objectForKey:@"0"];
            [_toDoItems addObject:item];
        }
        else{
            NSLog(@"Data is empty");
        }
    }
    else{
        NSLog(@"File not exists");
    }
}
else{
    NSLog(@"Directory not exists");
}

}

I am getting the above message "Directory not exists". This directory is the same as the one I referred to from interface A. But not able to access from interface B. Can anyone tell me what is the reason.

Thanks in advance

Upvotes: 0

Views: 103

Answers (2)

0yeoj
0yeoj

Reputation: 4550

Check your _appFile might be nil.

I have this methods for something like that, you can use something like this for moving into different class by simply implementing this kind of method in a custom nsobject class and call every time you need it..

- (BOOL)createWithName:(NSString *)fileName
{
    // make some writing here..

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSString *SearchPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

    NSString *FilePath = [SearchPath stringByAppendingPathComponent:fileName];

    BOOL createStatus = [fileManager fileExistsAtPath:FilePath];

    if (createStatus)
    {
        NSLog(@"Log: Created!");
    }
    else
    {
        NSLog(@"Log: Failed!");
    }

    return createStatus;
}

and this one for search file with path..

- (BOOL)findFileWithName:(NSString *)fileName inDirectory:(NSString *)directoryPath
{
    NSFileManager *fileManager = [NSFileManager defaultManager];

    // for staticly assigned directory path
    //
    //NSString *searchPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    //

    NSString *mergedPath = [directoryPath stringByAppendingPathComponent:fileName];

    BOOL isAvailable = [fileManager fileExistsAtPath:mergedPath];

    if (isAvailable)
    {
        NSLog(@"Log: File found");
    }
    else
    {
        NSLog(@"Log: No file found");
    }

    return isAvailable;
}

Upvotes: 0

Pravin Tate
Pravin Tate

Reputation: 1130

Follow the MVC pattern. Write one class which will do all stuff related files and just pass file name to that class and get file. (your loadData method will goes in that) because if one is access from one interface it must be access from another interface also.

Upvotes: 1

Related Questions