1
votes

I wanted to populate a array with images in the folder which I directly dragged from my computer(folder name in compute:Assets) to xcode project.

This is the code which I wrote for that...

NSArray *dataArray = [[NSArray alloc]init];
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Assets"];
NSLog(@"%@ SOURCE PATH",sourcePath);
NSError *error;
dataArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:sourcePath error:&error];
NSLog(@"error %@", error);
NSLog(@"%lu",(unsigned long)[self.dataArray count]);

But when I compile and run the application. The folder is not created in the app bundle, images inside it are just there in the app bundle outside of folder. And the folder Assets is not created in the app bundle.

The error I get is this:

error Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x7fbc4af19700 {NSFilePath=/Users/prajeetshrestha/Library/Developer/CoreSimulator/Devices/32774E43-6CAC-4091-B642-3001513F578A/data/Containers/Bundle/Application/E932A108-D07F-4985-A58D-2D3101F52D6A/TestPullData.app/Assets, NSUserStringVariant=( Folder ), NSUnderlyingError=0x7fbc4ad3ee90 "The operation couldn’t be completed. No such file or directory"}

Folder Structure in Xcode: http://i62.tinypic.com/mvgr4z.png

Folder Structure in App Bundle: http://i57.tinypic.com/34gsg3d.png

1
The last NSlog shows array count as 0 too . Since the folder is not created at all in the bundle.prajeet Shrestha
In Xcode, is the folder blue or yellow?rmaddy
It's yellow. You can see the xcode folder structure in above link.prajeet Shrestha
Yellow folders are just groups in your project. Their contents are copied directly to the resource bundle during the build. Blue folders are kept as-is when the app is built.rmaddy
How to make it blue ? I dragged and dropped the folder from my computer to the project. I did not create group from xcode.prajeet Shrestha

1 Answers

1
votes

Try like this:

Step 1: Create folder in your project directly & drag it into project by choosing create folder reference

enter image description here

Step 2: Now try this code

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Assets"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error = [[NSError alloc] init];
    NSArray *dataArray = [fileManager contentsOfDirectoryAtPath:path error:&error];
    NSLog(@"dataArray = %@", dataArray);
}