所以我需要添加用戶使用NSOpenPanel選擇的NSArray中的對象,並將所有文件名放入此數組中。然後我有一個名爲參數的NSMutableArray,我將以編程方式放置參數。然後我需要將這些對象從NSArray添加到此NSMutableArray的末尾。所以我使用[NSMutableArray addObjectsFromArray:NSArray]
,並不斷給我一個錯誤。如何將NSArray中的對象添加到NSMutableArray的末尾?
這是我的代碼做: 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嗎? – 2012-03-17 16:27:39
'addObjectsFromArray:'是將另一個數組中的對象添加到可變數組末尾的正確方法。另外,'argumentsFinal'是不必要的;每個NSMutableArray都是一個NSArray,所以你可以使用'self.arguments'來完成任務的參數。 – 2012-03-17 18:28:48
我沒有使用ARC – drewsdunne 2012-03-18 04:34:58