質問

I am trying to get selected files from bundle resourcePath on application launch. I am able to get all the files at resource path but when i try to get files with name stored in BGImage.plist i get error "cocoa 260" (item not found at specified path).

But images are present at the path and I can get all the images by doing [fileManager copyItemAtPath:bundlePath toPath:BGImagePath error:nil];

code below doesnt work.. (reference : iPhone (iOS): copying files from main bundle to documents folder error )

NSString* BGImagePath =[[[AppDelegate applicationDocumentsDirectory]
                         URLByAppendingPathComponent:@"BGImages" isDirectory:YES]
                        path];
NSFileManager* fileManager = [NSFileManager defaultManager];
NSError *error = nil;
NSArray *folderContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:BGImagePath error:&error];
NSString *bundlePath = [[NSBundle mainBundle] resourcePath];
NSString *BundleImagePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"BGImages.plist"];          //has file names 
bgImagesArray = [[NSArray arrayWithContentsOfFile:BundleImagePath] retain];

if (folderContents.count == 0) {
    for (id obj in bgImagesArray) {
            NSError* error;
            if ([fileManager
                  copyItemAtPath:[bundlePath :obj]
                  toPath:[BGImagePath stringByAppendingPathComponent:obj]
                  error:&error])
                NSLog(@" *-> %@", [bundlePath stringByAppendingPathComponent:obj]);
       }
    }
}

Is there any other elegant way to get specific files without storing names in plist?

役に立ちましたか?

解決

This following code fixed the issue:

 NSFileManager* fileManager = [NSFileManager defaultManager];
 NSString* bgImagePath =[[[AppDelegate applicationDocumentsDirectory]
                         URLByAppendingPathComponent:@"BGImages" isDirectory:YES]
                        path];
 [fileManager createDirectoryAtPath:bgImagePath withIntermediateDirectories:NO   attributes:nil error:nil];
  NSArray * folderContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bgImagePath error:nil];
 NSString * bundlePath = [[NSBundle mainBundle] resourcePath];
 NSString * bundleImagePath = [[NSBundle mainBundle] pathForResource:@"BGImages" ofType:@"plist"];
 NSArray* bgImagesArray = [NSArray arrayWithContentsOfFile:bundleImagePath];
 if (folderContents.count == 0) {
    [bgImagesArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSString * sourcePath = [bundlePath stringByAppendingPathComponent:obj];
        NSString * destinationPath = [bgImagePath stringByAppendingPathComponent:obj];
        [fileManager copyItemAtPath:sourcePath toPath:destinationPath error:nil];
    }];
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top