在命令行程序中使用NSBundle时,我注意到了一些奇怪的行为.如果,在我的程序中,我使用现有的bundle并制作它的副本,然后尝试使用pathForResource在Resources文件夹中查找某些内容,除非我在程序启动之前查找我正在查找的包,否则总是会返回nil.我创建了一个复制问题的示例应用程序,相关代码是:
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *exePath = [NSString stringWithCString:argv[0]
encoding:NSASCIIStringEncoding];
NSString *path = [exePath stringByDeletingLastPathComponent];
NSString *templatePath = [path stringByAppendingPathComponent:@"TestApp.app"];
// This call works because TestApp.app exists before this program is run
NSString *resourcePath = [NSBundle pathForResource:@"InfoPlist"
ofType:@"strings"
inDirectory:templatePath];
NSLog(@"NOCOPY: %@", resourcePath);
NSString *copyPath = [path stringByAppendingPathComponent:@"TestAppCopy.app"];
[[NSFileManager defaultManager] removeItemAtPath:copyPath
error:nil];
if ([[NSFileManager defaultManager] copyItemAtPath:templatePath
toPath:copyPath
error:nil])
{
// This call will fail if TestAppCopy.app does not exist before
// this …Run Code Online (Sandbox Code Playgroud)