【发布时间】:2011-06-16 17:25:41
【问题描述】:
我编写了以下函数来从目录中随机选择一个文件。当我在 Xcode 中构建项目以使用自动打开的应用程序发布时,它工作得很好。但是,如果我从 finder 打开应用程序,按下触发此功能的按钮将导致我的程序冻结然后崩溃。我唯一能想到的是将参数更改为 contentsOfDirectoryAtPath: 以不具有 ./,但任何一个版本都有相同的确切问题。
查看控制台告诉我,我的程序异常退出并出现浮点异常,但我不知道是什么原因造成的。有什么我没有看到的东西跳出来给你们吗?大约一周前我才开始学习/使用 Objective-C 和可可,所以这对我来说都是全新的。
感谢您查看此...
- (NSMutableString*)setFilePathRandom{
NSArray* files;
NSFileManager* fileManager;
fileManager = [[NSFileManager alloc] init];
files = [fileManager contentsOfDirectoryAtPath:@"./Random Trippy Pics" error:NULL];
NSString* directoryPath = (NSMutableString*)[fileManager currentDirectoryPath];
NSString* fileName;
do{
fileName = [files objectAtIndex:(arc4random()%[files count])];
}while([fileName isEqualToString:@".DS_Store"]);
filePath = [NSString stringWithFormat:@"%@/Random Trippy Pics/%@",directoryPath,fileName];
[fileManager release];
return filePath;
}
【问题讨论】:
标签: objective-c cocoa nsfilemanager