gpt4 book ai didi

objective-c - 调用 Objective-C 方法时访问错误

转载 作者:行者123 更新时间:2023-12-03 16:38:27 25 4
gpt4 key购买 nike

我是 Objective-c 的新手。我已经上过 python 和 c++ 类(class),但我对 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

最佳答案

NSFileManager *tempManager;

您没有创建 tempManager

应该是这样的

NSFileManager *tempManager=[[NSFileManager alloc] init];

如果您不使用 ARC,您有责任(自动)释放 tempManager。

或使用NSFileManager *tempManager = [NSFileManager defaultManager]

关于objective-c - 调用 Objective-C 方法时访问错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8661405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com