我是Objective-c的新手。我参加了python和c ++课程,但我对objective-c非常感兴趣所以我在过去的几天里一直在努力学习。该程序应该比较两个位置的文件并添加缺失的文件。虽然目前尚未实现所有功能,但是简单的同步。
错误来自recursivelyGetContentsOfDirectory函数。它返回错误EXEC_BAD_ACCESS。因为它实际上不在函数中,所以我假设它与它的定义有关。
#import <AppKit/AppKit.h>
@interface Sync : NSControl{
NSURL *baseToDirectory;
NSURL *baseFromDirectory;
NSArray *filesInBaseFromDirectory;
IBOutlet NSTextField *baseToDirectoryLabel;
IBOutlet NSTextField *baseFromDirectoryLabel;
}
- (IBAction)chooseBaseSyncFromDirectory:(id)sender;
- (IBAction)chooseBaseSyncToDirectory:(id)sender;
- (IBAction)performSync:(id)sender;
void recursivelyGetContentsOfDirectory (NSURL *path);
@end
实施档案
#import "Sync.h"
@implementation Sync
- (IBAction)chooseBaseSyncFromDirectory:(id)sender {
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
[openDlg setCanChooseDirectories:YES];
[openDlg setCanChooseFiles:NO];
//Open the directory
if ([openDlg runModal] == NSFileHandlingPanelOKButton) {
NSArray *files = [openDlg URLs];
NSString *fileAsString = [[files objectAtIndex:0] path];
[baseFromDirectoryLabel setStringValue:fileAsString];
baseFromDirectory = [files objectAtIndex:0];
recursivelyGetContentsOfDirectory(baseFromDirectory);
}
}
- (IBAction)chooseBaseSyncToDirectory:(id)sender {
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
[openDlg setCanChooseDirectories:YES];
[openDlg setCanChooseFiles:NO];
if ([openDlg runModal] == NSFileHandlingPanelOKButton) {
NSArray *files = [openDlg URLs];
NSString *fileAsString = [[files objectAtIndex:0] path];
[baseToDirectoryLabel setStringValue:fileAsString];
baseToDirectory = [files objectAtIndex:0];
}
}
void recursivelyGetContentsOfDirectory (NSURL *path){
NSFileManager *tempManager;
NSArray *contentsOfDirectory = [tempManager contentsOfDirectoryAtPath:[path path] error:nil];
for (int i = 0; i < (int)[contentsOfDirectory count] ; i = i+1) {
if ([[contentsOfDirectory objectAtIndex:i] isDirectory]) {
NSString *next = [NSString stringWithString:[path path]];
[next stringByAppendingString:[contentsOfDirectory objectAtIndex:i]];
NSURL *nextPath = [NSURL URLWithString:next];
recursivelyGetContentsOfDirectory(nextPath);
} else {
NSLog(@"Non-Directory Found");
}
}
}
- (IBAction)performSync:(id)sender {
}
@end
答案 0 :(得分:2)
NSFileManager *tempManager;
您没有创建tempManager
它应该像
NSFileManager *tempManager=[[NSFileManager alloc] init];
如果您不使用ARC,则负责(自动)释放tempManager。
或使用NSFileManager *tempManager = [NSFileManager defaultManager]