So I need to add the objects from an NSArray that the user has chosen using an NSOpenPanel and put all the filenames into this array. Then I have an NSMutableArray called arguments that I am putting the arguments programmatically. Then I need to add these objects from the NSArray to the end of this NSMutableArray. So I use [NSMutableArray addObjectsFromArray:NSArray] and that keeps giving me an error.
This is what I’m doing with the code:
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];
}
And this is the error I keep getting in the log. I can’t figure out why I’m getting this.
EXC_BAD_ACCESS(code=13,address=0x0)
This points to the line [arguments addObjectsFromArray:filenames];
I can only make out the first part about the selector and the instance but I don’t know what it means. Help…
Be consistent. To begin with, prefix all your instance variables with an underscore, not just some of them.
Then you won’t be able to do this anymore:
Note that on the very next line, you’re doing this:
Again, being consistent in using properties rather than directly accessing instance variables will help you avoid these kinds of errors. So rewrite the previous line to use the property, like so:
The compiler translates
self.arguments = someArginto[self setArguments:someArg]. In this case the setter method is needed to retain the object so that it won’t be deallocated while the reference is still stored in the_argumentsinstance variable.