Objective c 作为NSComboBox数据源的目录名数组

Objective c 作为NSComboBox数据源的目录名数组,objective-c,cocoa,Objective C,Cocoa,我在检索文件夹名称并将其作为组合框的项目发送时遇到一些问题 我的实际代码: NSError *errors = nil; NSString *pathForDirectory = @"/Folder/Folder/"; NSFileManager* fileManager = [NSFileManager defaultManager]; NSArray *contentsDirectory = [fileManager contentsOfDirectoryAtPath:

我在检索文件夹名称并将其作为组合框的项目发送时遇到一些问题

我的实际代码:

NSError *errors = nil;
NSString *pathForDirectory = @"/Folder/Folder/";
NSFileManager* fileManager = [NSFileManager defaultManager];
NSArray *contentsDirectory = [fileManager contentsOfDirectoryAtPath:
                             pathForDirectory error:&errors];
NSArray *Directory = [NSArray arrayWithObjects:contentsDirectory];

dataFromArray = [[NSMutableArray alloc] init];
[dataFromArray addObjectsFromArray:Directory];  

[self sortItemInMyComboBox:dataFromArray];
因此,如果NSArray*目录是用静态数组定义的,它可以工作,但是使用上面的代码,应用程序会崩溃,并出现日志错误:由于未捕获的异常“NSRangeException”而终止应用程序,原因:'-[NSCFArray objectAtIndex::::]:索引(2147483647(或可能更大))超出边界(3)

我想,我的错误是如何使用NSFileManager,但我尝试了其他方法,但没有成功

提前感谢,, 罗南。

问题 解决方案(有关更好的解决方案,请参见下文。) 理由
+(NSArray*)数组及其对象:
不包括以下以零结尾的列表:

NSArray *fruits = [NSArray arrayWithObjects:@"Apple", @"Grape", @"Banana", nil];
NSError *error = nil;
NSString *path = @"/Folder/Folder/";
NSFileManager* fileManager = [NSFileManager defaultManager];
NSArray *directory = [fileManager contentsOfDirectoryAtPath:pathForDirectory
                                                      error:&error];

if (error) {
    // do something about it
}

// You don't need autorelease if you use garbage collection
NSMutableArray *files = [[directory mutableCopy] autorelease];
// this looks like it violates the MVC pattern, have you
// thought about using bindings?
[self sortItemInMyComboBox:files];
如果找不到
nil
,它会尝试添加不存在的对象。这会导致一条丑陋的错误消息,而不一定是您遇到的错误消息


顺便说一句:

您的代码看起来太复杂了,应该是这样的:

NSArray *fruits = [NSArray arrayWithObjects:@"Apple", @"Grape", @"Banana", nil];
NSError *error = nil;
NSString *path = @"/Folder/Folder/";
NSFileManager* fileManager = [NSFileManager defaultManager];
NSArray *directory = [fileManager contentsOfDirectoryAtPath:pathForDirectory
                                                      error:&error];

if (error) {
    // do something about it
}

// You don't need autorelease if you use garbage collection
NSMutableArray *files = [[directory mutableCopy] autorelease];
// this looks like it violates the MVC pattern, have you
// thought about using bindings?
[self sortItemInMyComboBox:files];
问题 解决方案(有关更好的解决方案,请参见下文。) 理由
+(NSArray*)数组及其对象:
不包括以下以零结尾的列表:

NSArray *fruits = [NSArray arrayWithObjects:@"Apple", @"Grape", @"Banana", nil];
NSError *error = nil;
NSString *path = @"/Folder/Folder/";
NSFileManager* fileManager = [NSFileManager defaultManager];
NSArray *directory = [fileManager contentsOfDirectoryAtPath:pathForDirectory
                                                      error:&error];

if (error) {
    // do something about it
}

// You don't need autorelease if you use garbage collection
NSMutableArray *files = [[directory mutableCopy] autorelease];
// this looks like it violates the MVC pattern, have you
// thought about using bindings?
[self sortItemInMyComboBox:files];
如果找不到
nil
,它会尝试添加不存在的对象。这会导致一条丑陋的错误消息,而不一定是您遇到的错误消息


顺便说一句:

您的代码看起来太复杂了,应该是这样的:

NSArray *fruits = [NSArray arrayWithObjects:@"Apple", @"Grape", @"Banana", nil];
NSError *error = nil;
NSString *path = @"/Folder/Folder/";
NSFileManager* fileManager = [NSFileManager defaultManager];
NSArray *directory = [fileManager contentsOfDirectoryAtPath:pathForDirectory
                                                      error:&error];

if (error) {
    // do something about it
}

// You don't need autorelease if you use garbage collection
NSMutableArray *files = [[directory mutableCopy] autorelease];
// this looks like it violates the MVC pattern, have you
// thought about using bindings?
[self sortItemInMyComboBox:files];

+回答得好。您的大多数代码更改都是好的,但自动释放总是正确的-GC不会像在Retain Release下那样将其添加到自动释放池中,但收集器可以将其用作提示。此外,为了接触到最广泛的受众并能够使用任何第三方框架,最好编写在RR和GC.GS下都能工作的“双模式”代码:谢谢您的更正。正如您所建议的,我使用了绑定,因此最终结果如下:…NSMutableArray*files=[directory mutableCopy];[dataFromArray addObjectsFromArray:files];其中dataFromArray是NSComboxBox出口。与主控制器绑定(模型密钥路径:self.dataFromArray)。然后工作。再次感谢。罗南,回答得好。您的大多数代码更改都是好的,但自动释放总是正确的-GC不会像在Retain Release下那样将其添加到自动释放池中,但收集器可以将其用作提示。此外,为了接触到最广泛的受众并能够使用任何第三方框架,最好编写在RR和GC.GS下都能工作的“双模式”代码:谢谢您的更正。正如您所建议的,我使用了绑定,因此最终结果如下:…NSMutableArray*files=[directory mutableCopy];[dataFromArray addObjectsFromArray:files];其中dataFromArray是NSComboxBox出口。与主控制器绑定(模型密钥路径:self.dataFromArray)。然后工作。再次感谢。罗南。