Problem with my for loop?

Discussion in 'Mac Programming' started by trey5498, Jun 23, 2008.

  1. macrumors regular

    Joined:
    Jun 16, 2008
    #1
    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)?
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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.
     
  3. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #3
    Of course they will crash if you run some bad software on them. They just have a better chance of only the app crashing without bringing the whole system down (although admittedly Vista is pretty rock solid for me).
     
  4. thread starter macrumors regular

    Joined:
    Jun 16, 2008
    #4
    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?
     
  5. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    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!
     
  6. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #6
    Bah, ninja quote. I noticed my mistake and deleted my original post. I guess I didn't get away with it :).
     
  7. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    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.
     
  8. thread starter macrumors regular

    Joined:
    Jun 16, 2008
    #8
    This gave me the error of "error:syntax error before '=' token"
     
  9. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #9
    Really? Is that in your .h or .m file. I can't see any obvious stupid errors on my part...
     
  10. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #10
    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
     
  11. thread starter macrumors regular

    Joined:
    Jun 16, 2008
    #11
    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.
     
  12. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #12
    OK, not convinced you are are allowed to allocate an object in the header. Allocate it in the init method in the .m file.
     
  13. thread starter macrumors regular

    Joined:
    Jun 16, 2008
    #13
    error initializing all together whether it is in the .m or the .h

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

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #14
    Not as long as you are including Cocoa/Cocoa.h or similar.
     
  15. thread starter macrumors regular

    Joined:
    Jun 16, 2008
    #15
    I have that one included. What is a better way of doing the? I really need to figure this out lol
     
  16. macrumors 65816

    Sbrocket

    Joined:
    Jun 3, 2007
    Location:
    /dev/null
    #16
    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.
     
  17. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #17
    Post your entire .h and .m files. You need to be doing the allocation in a method like init: not just loose.
     
  18. macrumors 65816

    Sbrocket

    Joined:
    Jun 3, 2007
    Location:
    /dev/null
    #18
    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
     
  19. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #19
    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.
     
  20. macrumors 65816

    Sbrocket

    Joined:
    Jun 3, 2007
    Location:
    /dev/null
    #20
    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.
     
  21. thread starter macrumors regular

    Joined:
    Jun 16, 2008
    #21
    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
     
  22. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #22
    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...
     
  23. thread starter macrumors regular

    Joined:
    Jun 16, 2008
    #23
    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.
     
  24. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #24
    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.
     
  25. macrumors 65816

    Sbrocket

    Joined:
    Jun 3, 2007
    Location:
    /dev/null
    #25
    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.
     

Share This Page