Janosch Hübner
Janosch Hübner

Reputation: 1694

Objective C Accessing NSBundle

I have a few questions and some problems:

What I want to do:

Rename a File in MyApp.app/Assets/Debug/debug.xml to MyApp.app/Assets/Debug/debug_2.xml

1. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"debug" ofType:"xml" inDirectory:@"Assets"]; returns (null)

2. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"debug" ofType:"xml" inDirectory:@"Debug"]; returns (null)

3. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"debug" ofType:"xml"]; WORKS(it finds the file EVEN if its not in the .app its in .app/Assets/Debug.xml)

BUT:

    if ([fm fileExistsAtPath:path]) {

NSString *newPath = [[NSBundle mainBundle] pathForResource:@"newfile" ofType:@"xml"];   

            [fm copyItemAtPath:path toPath:newPath error:NULL];

        } else {
            NSLog(@"File does not exists");
        }

the code always throws this error:

-[NSFileManager copyItemAtPath:toPath:error:]: destination path is nil'

of course the file does not exists, but its the same path than the file path.. just with another name :)

I want to rename a file in .app folder with the same path just another name.

Any help appreciated!

Also in the example 1 and 2 above: why does it give me null path if I use inDirectory parameter? I added a folder to the build ressources and such.. i tested all

Thanks

Upvotes: 0

Views: 815

Answers (3)

Shebuka
Shebuka

Reputation: 3228

You need to read developer manuals for methods you use...

I'll interpret it for you:

pathForResource:ofType:inDirectory:

If inDirectory is nil, this method searches the top-level nonlocalized resource directory and the top-level of any language-specific directories. [cut] For example, suppose you have a Mac OS X application with a modern bundle and you specify @"Assets" for the subpath parameter. This method would first look in the Contents/Resources/Assets directory of the bundle...

and your file is not in Contents/Resources/Assets.

pathForResource: will not work for not existing files.

What you need to do:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"debug" ofType:"xml"];
NSString *newPath = [filePath stringByDeletingLastPathComponent];
newPath = [newPath stringByAppendingPathComponent:@"debug_2.xml"];

Upvotes: 0

Mike Weller
Mike Weller

Reputation: 45598

You cannot modify resources in the application bundle. It is essentially read-only. The pathForResource methods only return the paths of existing files.

You must write any new files to the documents or caches directory instead. Where exactly you put them depends on if they can be regenerated and if they should be backed up. See this documentation for more information.

Upvotes: 2

Dave
Dave

Reputation: 1081

If you want to create dir in the app sandbox:

applicationDataFolder = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0]; //OR NSDocumentsDirectory
    NSString* libraryFolderPath = [[[MAFCore sharedInstance] applicationDataFolder] stringByAppendingPathComponent:@"myFolder"];
    BOOL dirExists = [[NSFileManager defaultManager] fileExistsAtPath:libraryFolderPath];
    if(dirExists == NO){
        [[NSFileManager defaultManager] createDirectoryAtPath:libraryFolderPath withIntermediateDirectories:NO attributes:nil error:nil];
    }

Upvotes: 0

Related Questions