【发布时间】:2012-03-17 15:44:48
【问题描述】:
所以我需要添加用户使用 NSOpenPanel 选择的 NSArray 中的对象,并将所有文件名放入该数组中。然后我有一个名为 arguments 的 NSMutableArray,我以编程方式放置参数。然后我需要将这些对象从 NSArray 添加到这个 NSMutableArray 的末尾。所以我使用[NSMutableArray addObjectsFromArray:NSArray],这一直给我一个错误。
这就是我对代码所做的: AppDelegate.h
#import <Cocoa/Cocoa.h>
@interface ZipLockAppDelegate : NSObject <NSApplicationDelegate> {
IBOutlet NSTextField *input;
IBOutlet NSTextField *output;
IBOutlet NSTextField *password;
NSArray *filenames;
NSMutableArray *arguments;
NSArray *argumentsFinal;
}
@property (assign) IBOutlet NSWindow *window;
@property (retain) NSArray *filenames;
@property (copy) NSMutableArray *arguments;
- (IBAction)chooseInput:(id)sender;
- (IBAction)chooseOutput:(id)sender;
- (IBAction)createZip:(id)sender;
@end
AppDelegate.m
#import "ZipLockAppDelegate.h"
@implementation ZipLockAppDelegate
@synthesize window = _window;
@synthesize filenames;
@synthesize arguments;
- (IBAction)chooseInput:(id)sender {
NSOpenPanel *openZip = [[NSOpenPanel alloc] init];
[openZip setCanChooseFiles:YES];
[openZip setCanChooseDirectories:YES];
[openZip setCanCreateDirectories:NO];
[openZip setAllowsMultipleSelection:YES];
[openZip setTitle:@"Select All Files/Folders to be zipped"];
int result = [openZip runModal];
if (result == 1) {
filenames = [openZip filenames];
}
}
- (IBAction)createZip:(id)sender {
[progress startAnimation:self];
arguments = [NSMutableArray arrayWithObjects:@"-P", [password stringValue], [output stringValue], nil];
[self.arguments addObjectsFromArray:filenames];
argumentsFinal = [[NSArray alloc] initWithArray:self.arguments];
NSTask *makeZip = [[NSTask alloc] init];
[makeZip setLaunchPath:@"/usr/bin/zip"];
[makeZip setArguments:argumentsFinal];
[makeZip launch];
[makeZip waitUntilExit];
[progress stopAnimation:self];
}
这是我在日志中不断出现的错误。我不知道为什么我会得到这个。
EXC_BAD_ACCESS(code=13,address=0x0)
这指向[arguments addObjectsFromArray:filenames];这一行
我只能弄清楚关于选择器和实例的第一部分,但我不知道它是什么意思。帮助...
【问题讨论】:
-
设置数组时应该使用
-mutableCopy,而不是-copy。你在使用 ARC 吗? -
addObjectsFromArray:是将另一个数组中的对象添加到可变数组末尾的正确方法。另外,argumentsFinal是不必要的;每个 NSMutableArray 都是一个 NSArray,因此您可以使用self.arguments作为任务的参数。
标签: objective-c cocoa nsmutablearray nsarray