Objective c 类型的pathForResource返回nil

Objective c 类型的pathForResource返回nil,objective-c,null,nsbundle,mainbundle,Objective C,Null,Nsbundle,Mainbundle,这是我正在使用的代码 NSString* path = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"plist"]; if(path == nil) { NSLog(@"file not found"); } 当我运行这个程序时,它会打印控制台中找不到的文件 my file.plist位于我的cocoa项目的supporting files文件夹中。mainBundle在哪里查找文件。这让我很难受。我知道它会查找.app文件

这是我正在使用的代码

NSString* path = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"plist"];

if(path == nil)
{
NSLog(@"file not found");
}
当我运行这个程序时,它会打印控制台中找不到的文件


my file.plist位于我的cocoa项目的supporting files文件夹中。mainBundle在哪里查找文件。这让我很难受。我知道它会查找.app文件,但在开发应用程序时,doe mainBundle会在其中查找?

pathForResource:ofType:
只在
Resources
文件夹(和本地化文件夹)中查找。如果要将文件放入捆绑包中的专用文件夹,则需要使用
pathForResource:ofType:inDirectory:
,并提供目录名。

pathForResource:ofType:
仅查找
资源
文件夹(和本地化文件夹)。如果要将文件放入捆绑包中的专用文件夹,则需要使用
pathForResource:ofType:inDirectory:
,并提供目录名。

pathForResource:ofType:
仅查找
资源
文件夹(和本地化文件夹)。如果要将文件放入捆绑包中的专用文件夹,则需要使用
pathForResource:ofType:inDirectory:
,并提供目录名。

pathForResource:ofType:
仅查找
资源
文件夹(和本地化文件夹)。如果要将文件放入捆绑包中的专用文件夹,则需要使用
pathForResource:ofType:inDirectory:
,并提供目录名。

如果找到的
NSBundle
是您想要的:

NSBundle *yourTargetBundle = [NSBundle mainBundle];

or

NSBundle *yourTargetBundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] 
URLForResource:@"YourCustomBundleName" withExtension:@"bundle"]];
并使用函数:
pathForResource:of type:
获得nil结果。然后,您可以将资源的
路径记录在
yourTargetBundle
下:

// Given that your resource type is .png and have no sub dir
NSArray *resoursePaths = [yourTargetBundle pathsForResourcesOfType:@"png" inDirectory:nil];
NSLog(@"resoursePaths == %@",resoursePaths);
它将记录
png
类型的所有资源路径。您可以使用以下方法获取
图像
对象:

targetImg = [[UIImage alloc] initWithContentsOfFile:@"OnePngPathYouChose"];

我认为上述方法是解决
返回零
情况的一种方法。

如果您找到的
NSBundle
是您想要的:

NSBundle *yourTargetBundle = [NSBundle mainBundle];

or

NSBundle *yourTargetBundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] 
URLForResource:@"YourCustomBundleName" withExtension:@"bundle"]];
并使用函数:
pathForResource:of type:
获得nil结果。然后,您可以将资源的
路径记录在
yourTargetBundle
下:

// Given that your resource type is .png and have no sub dir
NSArray *resoursePaths = [yourTargetBundle pathsForResourcesOfType:@"png" inDirectory:nil];
NSLog(@"resoursePaths == %@",resoursePaths);
它将记录
png
类型的所有资源路径。您可以使用以下方法获取
图像
对象:

targetImg = [[UIImage alloc] initWithContentsOfFile:@"OnePngPathYouChose"];

我认为上述方法是解决
返回零
情况的一种方法。

如果您找到的
NSBundle
是您想要的:

NSBundle *yourTargetBundle = [NSBundle mainBundle];

or

NSBundle *yourTargetBundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] 
URLForResource:@"YourCustomBundleName" withExtension:@"bundle"]];
并使用函数:
pathForResource:of type:
获得nil结果。然后,您可以将资源的
路径记录在
yourTargetBundle
下:

// Given that your resource type is .png and have no sub dir
NSArray *resoursePaths = [yourTargetBundle pathsForResourcesOfType:@"png" inDirectory:nil];
NSLog(@"resoursePaths == %@",resoursePaths);
它将记录
png
类型的所有资源路径。您可以使用以下方法获取
图像
对象:

targetImg = [[UIImage alloc] initWithContentsOfFile:@"OnePngPathYouChose"];

我认为上述方法是解决
返回零
情况的一种方法。

如果您找到的
NSBundle
是您想要的:

NSBundle *yourTargetBundle = [NSBundle mainBundle];

or

NSBundle *yourTargetBundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] 
URLForResource:@"YourCustomBundleName" withExtension:@"bundle"]];
并使用函数:
pathForResource:of type:
获得nil结果。然后,您可以将资源的
路径记录在
yourTargetBundle
下:

// Given that your resource type is .png and have no sub dir
NSArray *resoursePaths = [yourTargetBundle pathsForResourcesOfType:@"png" inDirectory:nil];
NSLog(@"resoursePaths == %@",resoursePaths);
它将记录
png
类型的所有资源路径。您可以使用以下方法获取
图像
对象:

targetImg = [[UIImage alloc] initWithContentsOfFile:@"OnePngPathYouChose"];

我认为上述方法是解决
返回nil
情况的一种方法。

我厌倦了使用inDirectory路径和项目内部文件的本地路径,如:NSString*path=[[NSBundle mainBundle]pathForResource:@“file”of type:@“plist”inDirectory:@“/Users/joshuablevins/Documents/Xcode/Tutorials/plist-tut/plist-tut”];但它仍然说找不到文件。
inDirectory:
您只需在包中传递文件夹名称(支持文件文件夹名称),不是完整路径。这应该是您在复制项目生成阶段中设置的文件夹名称。如果您有文件的完整显式路径,那么它通常不在捆绑包中,并且您不使用NSBundle访问它。它应该是复制文件还是复制捆绑包资源生成阶段?复制文件(因为它允许您设置要复制到的文件夹名称).Copy bundle resources将所有内容放在
resources
folderI中,使用独立目录路径和项目内部文件的本地路径,如下所示:NSString*path=[[NSBundle mainBundle]pathForResource:@“file”of type:@“plist”独立目录:@”/Users/joshuablevins/Documents/Xcode/Tutorials/plist-tut/plist-tut];但它仍然显示找不到文件。
inDirectory:
您只需在包中传递文件夹名称(支持文件文件夹名称),不是完整路径。这应该是您在复制项目生成阶段中设置的文件夹名称。如果您有文件的完整显式路径,那么它通常不在捆绑包中,并且您不使用NSBundle访问它。它应该是复制文件还是复制捆绑包资源生成阶段?复制文件(因为它允许您设置要复制到的文件夹名称).Copy bundle resources将所有内容放在
resources
folderI中,使用独立目录路径和项目内部文件的本地路径,如下所示:NSString*path=[[NSBundle mainBundle]pathForResource:@“file”of type:@“plist”独立目录:@”/Users/joshuablevins/Documents/Xcode/Tutorials/plist-tut/plist-tut];但它仍然显示找不到文件。
inDirectory:
您只需在包中传递文件夹名称(支持文件文件夹名称),不是完整路径。这应该是您在复制项目生成阶段中设置的文件夹名称。如果您有文件的完整显式路径,那么它通常不在捆绑包中,并且您不使用NSBundle访问它。它应该是复制文件还是复制捆绑包资源生成阶段?复制文件(因为它允许您设置要复制到的文件夹名称).Copy bundle resources将所有内容放在
resources
folderI中,使用独立目录路径和项目内部文件的本地路径,如下所示:NSString*path=[[NSBundle mainBundle]pathForResource:@“file”of type:@“plist”独立目录:@”/Users/joshuablevins/Documents/Xcode/Tutorials/plist-tut/plist-tut];