kolaj
kolaj

Reputation: 1

pathForResource doesn't return an array

I have 2 methods, one for saving an array to a file and one for loading an array from the file, but when I'm trying to read an array, nothing happens, like an array is empty. My code:

- (IBAction)write:(id)sender
{
    NSArray *array = [racersArray copy];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libraryDirectory = [paths objectAtIndex:0];
    NSString *location = [libraryDirectory stringByAppendingString:@"/history.plist"];
    [array writeToFile:location atomically:YES];
}

- (IBAction)read:(id)sender
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"plist"];
    NSArray *array = (path != nil ? [NSArray arrayWithContentsOfFile:@"/history.plist"] : nil);
}

Upvotes: 0

Views: 93

Answers (4)

gnasher729
gnasher729

Reputation: 52632

Took a while to figure out that I don't actually know what you are trying to do.

pathForResource gives you paths for resource in your application bundle. You can only read from there. You can never write there. Sometimes people have the initial version of a file in the application bundle and then write it somewhere else. If that's what you want then most likely you need to:

(1) write a method that returns the path where the array will be stored once your application has been running.

(2) The "write" method calls this path method to know where to write the array.

(3) The "read" method calls this path method to know where to read the array, then you read it. However, the array can be nil because your app was run the first time, or the array wasn't written correctly. In that case, you use pathForResource to get the path where the file would be in your application bundle, and read it from there.

Upvotes: 0

Subbu
Subbu

Reputation: 62

- (IBAction)savetapped:(id)sender {
    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"json.geojson"];
    [paths writeToFile:plistPath atomically:YES];
}
- (IBAction)retriveTapped:(id)sender {
    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0]; NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"json.geojson"];
    NSLog(@"%@",documentsPath);
    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
    {
        plistPath = [[NSBundle mainBundle] pathForResource:@"json" ofType:@"geojson"];
    }
    NSLog(@"%@",plistPath );
}

Upvotes: 0

Subbu
Subbu

Reputation: 62

Try with this Methods may be helps you.👍

For Save

-(IBAction)save_Action:(id)sender{
    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"manuallyData.plist"];
    [plistData writeToFile:plistPath atomically:YES];
}

For Get Path

-(IBAction)retrive:(id)sender{
    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"manuallyData.plist"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
    {
         plistPath = [[NSBundle mainBundle] pathForResource:@"manuallyData" ofType:@"plist"];
    }
}

Upvotes: 0

trojanfoe
trojanfoe

Reputation: 122458

You are reading/writing from/to two different places.

  • You write to the Library folder (it should be under Library/Application Support or some such, but I digress).
  • You read from the app bundle.

Upvotes: 5

Related Questions