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
the last question was a good link for me to read there Sbrocket. The question however was answered by robbie who went above and beyond simply telling em to read as I would understand him if he just saying this. I do not want you to "do" my project as this will be my job and I need to know how. This project is past over due because of the person I am taking over this for. I simple need guidance and maybe a little looking over my code to see what I did wrong.

Again I appreciate all the help.
 

Cromulent

macrumors 604
Oct 2, 2006
6,813
1,100
The Land of Hope and Glory
the last question was a good link for me to read there Sbrocket. The question however was answered by robbie who went above and beyond simply telling em to read as I would understand him if he just saying this. I do not want you to "do" my project as this will be my job and I need to know how. This project is past over due because of the person I am taking over this for. I simple need guidance and maybe a little looking over my code to see what I did wrong.

Again I appreciate all the help.

Your doing this as a commercial project?
 

trey5498

macrumors regular
Original poster
Jun 16, 2008
191
0
no as an educational project to keep the students from screwing up on the printer installs. Each student can download for free once I have done it. It was just promised a month ago and I finally got it thrown into my hands
 

trey5498

macrumors regular
Original poster
Jun 16, 2008
191
0
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...

This built correctly, however it still does the same issue and crashes and never works.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
This built correctly, however it still does the same issue and crashes and never works.

Well, all your previous errors in the .m file still need to be corrected. You should:

1) Not re-initialise arrPRN
2) Ensure you are using the correct NSMutableArray methods (see my previous link) to add/remove items from the array
3) Check that items you read from the array are not nil before using them for anything.

Edit to add: as before: read some basic intro to Cocoa and probably C books/tutorials
 

trey5498

macrumors regular
Original poster
Jun 16, 2008
191
0
I have tried every method it seems on that page, I have to be missing something. I have tried initializing the array in almost every way. The way you just suggested doesn't seem to work as I get the same problem that everytime I try to access the array it locks up. I have tried in 2 places (I actually did try it in 2 places before you suggested it as I have 2 books that will be posted at the bottom):

Code:
NSMutableArray * arrPRN = [[NSMutableArray alloc] init];

when placed in the .m file it yields an error: " initializer element is not constant" and when in the .h file it yields an error: "syntax error before '=' token".

I understand that this is basic stuff and me being thrown into it kinda defeats the purpose, however when I code the things exactly how apple does and everyone else that knows what they are doing and I get all kinds of errors, I tend to think I need to reach out for help.


there has to be one small thing I have missed. This thing is frustrating me because ever site and ever book I pick up shows the above code I am using and it does not help or work. I get those errors and can't do anything else.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
You are missing the most basic understanding of C (let alone Objective-C or Cocoa). That is the root problem.

Declare the variable in the header.

Initialise the variable in the init. Do not redeclare the variable. This will simply be local to the method call you are in and will not set the class-level variable.

If you are still using arrPRN[] syntax anywhere stop. It's totally wrong.

In all seriousness you need to go back to basics and learn what you are doing. You are a danger at the moment.
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
Hi Trey5498

If you are pushed to get this app out quickly, and considering that you are not familiar with Objective-C and Cocoa, perhaps Cocoa isn't the most appropriate tool for the job. If you are familiar with VisualBASIC then I would suggest taking a look at RealBasic for the Mac. It will do what you need and even if you haven't used VisualBasic etc it will be a lot easier to 'learn on the job' than Objective-C and Cocoa.

b e n
 

trey5498

macrumors regular
Original poster
Jun 16, 2008
191
0
I guess this is where my Perl has overwritten anything I ever learned in C/C++.

in Perl you would use arrPRN;

in mac you use: anArray objectAtIndex:0?


so the for loop would be?:

Code:
for (i=0; i<[arrPRN count]; i++) {
    NSLog(@"%@", [arrPRN objectAtIndex: 0];
}

Does it help for you to know that all my C training and experence was in Windows and not in the world of Macs? Every C++ book I have say arrPRN which would explain my confusion if Macs are that much different. Again I do apologize for my ignorance as I am trying to learn under pressure and on the run. I think I am doing pretty darn good to get as far as I have lol.
 

trey5498

macrumors regular
Original poster
Jun 16, 2008
191
0
I will try that out. I want to learn all I can, and I am just as eager to learn this as I was in the first. I relish the learning and again, I haven't done that bad considering. I normally have valid code to ask for interpretation of instead of asking how do I do this. I try to read the few books I have and google.

I just need a good nudge in the right directions sometimes it is more of a blatant shove.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
in Perl you would use arrPRN;

in mac you use: anArray objectAtIndex:0?


It's not in Mac, it's in Objective-C/Cocoa. You can use other languages to write Mac software (including Perl with the correct libraries installed).

And the answer is still no. If you had an array the arrPRN syntax would be fine. But you don't have an array: you have an object. That object implements methods that allow it to behave like an unbounded array but it's not an array.

Code:
for (i=0; i<[arrPRN count]; i++) {
    NSLog(@"%@", [arrPRN objectAtIndex: 0];
}

Should work, but you will print the first element every time.
 

trey5498

macrumors regular
Original poster
Jun 16, 2008
191
0
meant this:

Code:
for (i=0; i<[arrPRN count]; i++) {
    NSLog(@"%@", [arrPRN objectAtIndex: i];
}

tried it with:

Code:
NSLog(@"%@", [arrPRN objectAtIndex: 0];
NSLog(@"%@", [arrPRN objectAtIndex: 1];
NSLog(@"%@", [arrPRN objectAtIndex: 2];

and it gave me: "-[NSCFArray objectAtIndex:]: index (1) beyond bounds (1)" Which I am assuming that it has something to do with maybe not setting an index or my syntax in adding the the objects to the array. I am using an for every check box.

Code:
arrPRN = [NSMutableArray new];
[arrPRN addObject:@"Printer Name"];

seems to workt the first with no problem, code I had there and never took a look at. Guess next thing would be to look at that.

At least I am heading in the right direction I guess lol. I love challenges but I somehow think I will die early because of them.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
meant this:

Code:
for (i=0; i<[arrPRN count]; i++) {
    NSLog(@"%@", [arrPRN objectAtIndex: i];
}

tried it with:

Code:
NSLog(@"%@", [arrPRN objectAtIndex: 0];
NSLog(@"%@", [arrPRN objectAtIndex: 1];
NSLog(@"%@", [arrPRN objectAtIndex: 2];

and it gave me: "-[NSCFArray objectAtIndex:]: index (1) beyond bounds (1)" Which I am assuming that it has something to do with maybe not setting an index or my syntax in adding the the objects to the array. I am using an for every check box.

Code:
arrPRN = [NSMutableArray new];
[arrPRN addObject:@"Printer Name"];

seems to workt the first with no problem, code I had there and never took a look at. Guess next thing would be to look at that.

At least I am heading in the right direction I guess lol. I love challenges but I somehow think I will die early because of them.

I don't know if this is going to be the point that pushes you into getting this going the right way... but arrPRN must be declared once in your header, and initialized once in your init method. You are getting a new bit of memory each time the code is run, so only the last addObject matters. Essentially the code that will run is:

Code:
//From init
arrPRN = [[NSMutableArray alloc] init];
//From subsequent calls to your action
arrPrn = [NSMutableArray new];
[arrPRN addObject:@"ItemA"];
arrPrn = [NSMutableArray new];
[arrPRN addObject:@"ItemB"];
arrPrn = [NSMutableArray new];
[arrPRN addObject:@"ItemC"];
arrPrn = [NSMutableArray new];
[arrPRN addObject:@"ItemD"];
arrPrn = [NSMutableArray new];
[arrPRN addObject:@"ItemE"];
NSLog(@"%@", [arrPRN objectAtIndex: 0]; //ItemE will display
NSLog(@"%@", [arrPRN objectAtIndex: 1]; //This will crash, there is only 1 item

At the end, ItemE will be the only one in the list.

Do not access with fixed indicies, this is asking for a seg fault. The first thing you did was OK, but you could also save yourself some trouble and use fast enumeration:
http://developer.apple.com/document.../ObjectiveC/Articles/chapter_7_section_1.html

This will prevent you from violating bounds and save you from having to declare throw-away variables. If you aren't comfortable with that, using a temporary variable from 0-[myObject count] is fine too.

What you want the code to look like is:
Code:
//From init
arrPRN = [[NSMutableArray alloc] init];
//From subsequent calls to your action
[arrPRN addObject:@"ItemA"];
[arrPRN addObject:@"ItemB"];
[arrPRN addObject:@"ItemC"];
[arrPRN addObject:@"ItemD"];
[arrPRN addObject:@"ItemE"];
for(NSString tmpString in arrPrn) {
  NSLog(@"%@",tmpString);
}

Take the new messages out. You don't want them, and you don't need them. You only need to allocate once, and that should be done with an alloc/init pair of messages in your init method.

-Lee
 

trey5498

macrumors regular
Original poster
Jun 16, 2008
191
0
ok the error is in the initiation of the array. I have it set up in the header as

Code:
NSMutatableArray *arrPRN;

and I have in the main file of :

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

Compiles fine with a warning about dealloc not being called, but I know that is because I must call the function. With the for loop now functioning correctly i notice that it still only has one element in the array. Am I correct that a mutablearray is supposed to allow you add an subtract elements from the array with out indicating an index total? How did I manage to mess up the initializer yet again lol. Everything else works.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
did you take out the new messages? If so, add some NSLog messages around each call to addObject, perhaps printing the count before and after the calls.

-Lee
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Compiles fine with a warning about dealloc not being called, but I know that is because I must call the function.

You should never call dealloc yourself. It's complaining because you are not chaining to the superclass dealloc like this:

Code:
-(void) dealloc
{
[super dealloc];
[arrPRN release];
}
 

trey5498

macrumors regular
Original poster
Jun 16, 2008
191
0
did you take out the new messages? If so, add some NSLog messages around each call to addObject, perhaps printing the count before and after the calls.

-Lee

I did take out all the [NSMutableArray New]
 

trey5498

macrumors regular
Original poster
Jun 16, 2008
191
0
Thank you for your time. Again I, in no way want it done for me as I need to learn it, but knowledge of what I am doing wrong will help me learn.

Thank you again for your time. And the DvrChk is not being called at this time because I haven't gotten to it yet lol.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
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?

It's not so much a screw up, it seems like a complete lack of understanding what you are doing.

arrPRN is a pointer to an object of type NSMutableArray. A pointer to an object should be initialised to NULL until an actual object is created, and then the pointer to that object should be stored here. Question: How often do you call [NSMutableArray new]? Second question: How often _should_ you call [NSMutableArray new]?

Now look up the documentation for NSMutableArray and find out how you access the objects stored in an NSMutableArray. Hint: It doesn't involve the [] operator. It would be useful if you took a C book and figured out what for example the expression arrPRN [15] actually means. It is frighteningly different from what you think it means.

Lastly about your comment "I thought Macs never crash". It is of course not true that Macs _never_ crash, they just don't do it very often unless your hardware is broken. But I am quite sure that running your program didn't crash the Mac at all. I am 99.9999% sure it was _only_ your program that crashed, not the Macintosh. Think of it as a security feature: The Macintosh protects its users from buggy code.
 

trey5498

macrumors regular
Original poster
Jun 16, 2008
191
0
gnasher,

have you read all the posts here? A lot of this has been cover from A to Z. I do appreciate your input, however I am not an idiot as one may paint me, just under pressure an asking for advice on my code, not for someone to do it for me.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
OK I've found why you only ever have 1 item in your NSMutableArray. And it's a basic logic error that would be the same in Perl so no defence here.

You have code like
Code:
	if([arc118clq state]==NSOnState) {
		[arrPRN addObject:@"ARC-118-CLQ"];
		
	 } else if([cse211clq state]==NSOnState) {
		[arrPRN addObject:@"CSE-211-CLQ"];
		
	 }

So if the first if statement is true the second will never get called and so on (the else).

You need to remove all those elses.

In general the whole thing is a mess. It doesn't seem to follow any OSX interface conventions and is really weird.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.