【问题标题】:Why does my app bundle not creating a folder?为什么我的 app bundle 没有创建文件夹?
【发布时间】:2015-01-01 03:56:08
【问题描述】:

我想用我直接从我的计算机(compute:Assets 中的文件夹名称)拖到 xcode 项目的文件夹中的图像填充一个数组。

这是我为此编写的代码...

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]);

但是当我编译并运行应用程序时。该文件夹不是在应用程序包中创建的,其中的图像就在文件夹外的应用程序包中。并且应用程序包中没有创建文件夹 Assets。

我得到的错误是这样的:

error Error Domain=NSCocoaErrorDomain Code=260 “操作无法完成。(Cocoa 错误 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=( 文件夹 ), NSUnderlyingError=0x7fbc4ad3ee90 "操作无法完成。没有这样的文件或目录"}

Xcode 中的文件夹结构: http://i62.tinypic.com/mvgr4z.png

App Bundle 中的文件夹结构: http://i57.tinypic.com/34gsg3d.png

【问题讨论】:

  • 最后一个 NSlog 也显示数组计数为 0。由于文件夹中根本没有创建文件夹。
  • 在 Xcode 中,文件夹是蓝色还是黄色?
  • 它是黄色的。您可以在上面的链接中看到 xcode 文件夹结构。
  • 黄色文件夹只是您项目中的组。它们的内容在构建期间直接复制到资源包中。构建应用程序时,蓝色文件夹保持原样。
  • 如何使它变蓝?我将文件夹从我的计算机拖放到项目中。我没有从 xcode 创建组。

标签: ios objective-c nsfilemanager nsbundle


【解决方案1】:

试试这样:

第 1 步: 直接在项目中创建文件夹并通过选择创建文件夹引用

将其拖到项目中

第 2 步:现在试试这段代码

- (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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-01
    • 2016-02-05
    • 2015-11-01
    • 2020-06-05
    相关资源
    最近更新 更多