Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

trey5498

macrumors regular
Original poster
Jun 16, 2008
191
0
I am no idiot when it comes to for loops. I have to use them all the time when it comes to Perl and I KNOW the syntax. How on earth is this syntax wrong and cause the program to crash. And I thought Macs weren't supposed to Crash?

Code:
- (IBAction)NextButton:(id)sender 
{
	if([arc118clq state]==NSOnState) {
		// printerarray[i] = Printer1;
		arrPRN = [NSMutableArray new];
		[arrPRN addObject:@"ARC-118-CLQ"];
		
	 } else if([cse211clq state]==NSOnState) {
		// printerarray[i] = Printer1;
		//arrPRN = [NSMutableArray new];
		[arrPRN addObject:@"CSE-211-CLQ"];
		
	 } else if([hubatlabclq state]==NSOnState) {
		// printerarray[i] = Printer1;
		//arrPRN = [NSMutableArray new];
		[arrPRN addObject:@"HUB-ATLAB-CLQ"];

	 } else if([nrnatlabclq state]==NSOnState) {
		// printerarray[i] = Printer1;
		//arrPRN = [NSMutableArray new];
		[arrPRN addObject:@"NRN-ATLAB-CLQ"];

	 } 

        int ind;
        for(ind=0; ind<17; ind++) {
             NSLog(@"%@", arrPRN[ind]);
        }
        [prnSelector orderOut:nil];
        [prnOverview makeKeyAndOrderFront:nil];
}

basically I am tring to print the array to NSLog to test to see if the addObject is adding it correctly. I am attempting step by step to eventually print this to a text field screen, which I don't know how to do either. So Question number one, why is the for loop screwing everthing up and if I take it out why does it function correctly? Question number two: How do I set this to show up in a text field (the class is NSScrollView)?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Where did the 17 come from? What do you thing arrPRN[17] will do? arrPRN is not an array: it's an object. You are using it as a pointer array using that syntax and will be readying into "random" memory. Go back and read some Cocoa and C basics.
 

trey5498

macrumors regular
Original poster
Jun 16, 2008
191
0
maybe I should have been a tad bit more specific....

the 17 is the fact that there is 17 check boxes it is checking, I just didn't want to put all 17 there to clutter up stuff, however all are coded in the exact same way. the array is declared in the header file as:

Code:
NSMutableArray *arrPRN;

If this is done incorrectly, I have coded it the exact same way that the developer site of apple has declaring arrays. So how did I screw up?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
That is not an array. It is a pointer to type NSMutableArray. An array would be declared like this:

Code:
NSMutableArray arrPRN[17];

Note the above array has 18 elements, not 17 as 0 is considered to be an element in arrays.

And is an array of NSMutableArrays. Which would be quite unusual. As he's adding NSStrings to his NSMutableArray would it not be better to declare an array of NSStrings? Or even pointers to NSStrings...

Edit: damn, Cromulent seems to have deleted his post!
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
maybe I should have been a tad bit more specific....

the 17 is the fact that there is 17 check boxes it is checking, I just didn't want to put all 17 there to clutter up stuff, however all are coded in the exact same way. the array is declared in the header file as:

Code:
NSMutableArray *arrPRN;

If this is done incorrectly, I have coded it the exact same way that the developer site of apple has declaring arrays. So how did I screw up?

Ok the problem here seems to be that you are used to Perl. Which will allow you to make mistakes and try and access memory you have not initialised/used. C and, therefore, Objective-C will not. And it will cause your code (not your Mac) to crash.

Your array is an Object. So you declare it something like:
Code:
NSMutableArray *arrPRN = [[NSMutableArray alloc] init];

You can then use the methods in the documentation to add and remove elements from the array. You cannot use arrPRN[index]. This offsets through RAM to RAM you do not own.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
This thread has pretty much covered the right way to do what you're wanting to do, but I thought I'd toss out there what your code actually did, since C is so different in this regard than perl (though you could do something similar with arrayrefs, i suppose).

Any pointer in C can have the array subscript operator, [] applied to it. The first operand, the pointer, has the address it points to incremented by the size of the type it points to multiplied by the second operand, which is the number between the square brackets. In this case, the pointer arrPRN points to the memory allocated by the new message. That address is taken, then 0 to 16*sizeof(NSMutableArray) is added to that address. The resulting memory pointed to by this address is returned as an NSMutableArray, and passed to NSLog. This is, as I'm sure you know by now, not at all what you wanted to happen.

Good luck applying what others in this thread explained.

-Lee
 

trey5498

macrumors regular
Original poster
Jun 16, 2008
191
0
Code:
NSMutableArray *arrPRN = [[NSMutableArray alloc] init];

is in the header file and the error is located in the .m file. I do apologize for my ignorance in this as I am over taken this task from a coworker that TRULY has no idea. I have made leaps and bounds coming from a Perl man.
 

trey5498

macrumors regular
Original poster
Jun 16, 2008
191
0
error initializing all together whether it is in the .m or the .h

Stupid question? Do I need to include the NSArray.h?
 

trey5498

macrumors regular
Original poster
Jun 16, 2008
191
0
I have that one included. What is a better way of doing the? I really need to figure this out lol
 

Sbrocket

macrumors 65816
Jun 3, 2007
1,250
0
/dev/null
error initializing all together whether it is in the .m or the .h

Stupid question? Do I need to include the NSArray.h?

Not NSArray.h specifically, no, but there should be somewhere in your code (possibly a .pch file, or if not somewhere else) that includes <Foundation/Foundation.h>. The Foundation framework will include all your base classes like NSArray, NSString, their mutable subclasses, and so on.

Normally Xcode does that kind of thing for you automatically when you start a new project with a Cocoa template, so if you're rolling your own project from scratch you'll need to include Foundation, add the framework to your project, and make sure things are building correctly.

Edit: You people are too quick. Yeah, <Cocoa/Cocoa.h> will work just fine too.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Well he doesn't have to, no. Not if he's just going to be using that object inside that method anyways.

And dear god, stop typing so fast. :eek:

:p

I think he does: if you just put something like this in a .m file you get a compiler error (assuming the variable has been declared in a header file):

Code:
@implementation MyClass
arrPTR = [[NSMutableArray alloc] init];
@end

It appears that he wants this variable to be an instance variable for the class available for re-use every time the method is called. So it should be in init.
 

Sbrocket

macrumors 65816
Jun 3, 2007
1,250
0
/dev/null
Code:
@implementation MyClass
arrPTR = [[NSMutableArray alloc] init];
@end

It appears that he wants this variable to be an instance variable for the class available for re-use every time the method is called. So it should be in init.

Well no, just sticking it willy nilly in the middle of nowhere isn't right. I was under the impression it was just some temp variable he was using in that method, not an instance variable. Hence the confusion.
 

trey5498

macrumors regular
Original poster
Jun 16, 2008
191
0
Code:
import <Cocoa/Cocoa.h>

@interface AppController : NSObject {
	
	NSMutableArray *arrPRN = [[NSMutableArray alloc] init];;	
	
	//NSArray *arrPRN;
    NSWindow *prnOverview;
    NSWindow *prnSelector;
	// Plotters
    IBOutlet NSButton *cse211djq;
	IBOutlet NSButton *arc118djq;
	// Color Printer
	IBOutlet NSButton *cse211clq;
	IBOutlet NSButton *arc118clq;
	IBOutlet NSButton *hubatlabclq;
	IBOutlet NSButton *nrnatlabclq;
	IBOutlet NSButton *wei408clq;
}
- (IBAction)NextButton:(id)sender;
- (IBAction)BackButton:(id)sender;
@end

The is a variable that will be used in two places. When the user clicks next it will print it into a NSScrollView so the user can recap what he selected and then again to know what subroutine to call to install.


the comments are me attempt to play around to try different things
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Change the .h to this:

Code:
import <Cocoa/Cocoa.h>

@interface AppController : NSObject {
	
	NSMutableArray *arrPRN;	
	
	//NSArray *arrPRN;
    NSWindow *prnOverview;
    NSWindow *prnSelector;
	// Plotters
    IBOutlet NSButton *cse211djq;
	IBOutlet NSButton *arc118djq;
	// Color Printer
	IBOutlet NSButton *cse211clq;
	IBOutlet NSButton *arc118clq;
	IBOutlet NSButton *hubatlabclq;
	IBOutlet NSButton *nrnatlabclq;
	IBOutlet NSButton *wei408clq;
}
- (IBAction)NextButton:(id)sender;
- (IBAction)BackButton:(id)sender;
@end

Edit:

Add

Code:
-(id) init
{
if ([super init])
{
arrPRN = [[NSMutableArray alloc] init];
}
return self;
}

-(void) dealloc
{
[arrPRN release];
}
to .m

But in all seriousness this is Cocoa 101. Please go and read some basic documentation...
 

trey5498

macrumors regular
Original poster
Jun 16, 2008
191
0
ok, good link on how to convert this onto the NSScrollView? If I was not being rushed because the other person dropped the ball I wouldn't be asking so much.

I do appreciate all the help.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
ok, good link on how to convert this onto the NSScrollView?

You can't. A NSScrollView simply holds another view and allows scrolling of that view via a NSClipView contained within. You need to click through further/harder in Interface Builder to get to the actual NSTextView contained within. Once you have a connection to the text view (not the scroll view or the clip view) you can simply use the insertText: method.
 

Sbrocket

macrumors 65816
Jun 3, 2007
1,250
0
/dev/null
ok, good link on how to convert this onto the NSScrollView? If I was not being rushed because the other person dropped the ball I would be asking so much.

I do appreciate all the help.

That question doesn't even make any sense.

I would take robbieduncan's advice and go read up on Cocoa 101. We aren't here to do your project or whatever this is for you.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.