Some cocoa help:P

Discussion in 'General Mac Discussion' started by mmmdreg, Sep 14, 2003.

  1. mmmdreg macrumors 65816

    mmmdreg

    Joined:
    Apr 14, 2002
    Location:
    Sydney, Australia
    #1
    Okay. I have *no* idea on how to use obj-c (code-wise). I only know my VB so could someone please translate from the following code into obj-c so I can just get a taste?

    The "-"'s are the indentations cos this can't recognise space's it seems.
     
  2. GeeYouEye macrumors 68000

    GeeYouEye

    Joined:
    Dec 9, 2001
    Location:
    State of Denial
    #2
    I think I can translate it (or everything but the listbox.additem - no idea what that is), but it'll be the C part of Objective-C (but since O-C is a true superset, unlike C++, any code valid in C is valid in O-C)

    int a, b, c;
    char foo[3];
    for (a=1; a <=26; a++)
    --for (b = 1; b <=26; b++)
    ----for (c=1; c<=26; c++)
    ----{
    ------foo[0]=(a+96);
    ------foo[1]=(b+96);
    ------foo[2]=(c+96);
    ----}

    Again, I could be wrong, and I definitely don't know what the listbox.additem is (I've never used VB, in fact the only BASIC I've ever seriously done is TI-BASIC), but the for loop is pretty evident.
     
  3. szark macrumors 68030

    szark

    Joined:
    May 14, 2002
    Location:
    Arid-Zone-A
    #3
    Re: Some cocoa help:p

     
  4. mmmdreg thread starter macrumors 65816

    mmmdreg

    Joined:
    Apr 14, 2002
    Location:
    Sydney, Australia
  5. szark macrumors 68030

    szark

    Joined:
    May 14, 2002
    Location:
    Arid-Zone-A
    #5
    Oops! Just noticed a major mistake in mine and corrected it. :)
     
  6. mmmdreg thread starter macrumors 65816

    mmmdreg

    Joined:
    Apr 14, 2002
    Location:
    Sydney, Australia
    #6
    Maybe I'm just a total n00b but I get 2 errors and a warning:
    regarding the line NSString foo;

    regarding the line foo = [NSString stringWit...; and

    in reference to itself
     
  7. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #7
    I believe that changing NSString foo to NSString *foo will fix the first two problems since NSString stringWithFormat is trying to return a reference to an NSString object which would be held in an NSString pointer.
     
  8. szark macrumors 68030

    szark

    Joined:
    May 14, 2002
    Location:
    Arid-Zone-A
    #8
    My apologies for the mistakes -- I previously typed up a quick example and did not try to verify that it actually worked correctly. I also somehow coded for an NSComboBox instead of an NSTableView.

    I've really gotta brush up on my Cocoa skills... ;)

    Here is a tested, working example:

    Code:
    //
    //  Controller.h
    //  ListBoxTest
    //
    //
    
    #import <Cocoa/Cocoa.h>
    
    
    @interface Controller : NSObject {
    
        IBOutlet NSTableView	*listBox;
    
        NSMutableArray		*listElements;
    
    }
    
    @end
    
    Code:
    //
    //  Controller.m
    //  ListBoxTest
    //
    //
    
    #import "Controller.h"
    
    
    @implementation Controller
    
    - (void)awakeFromNib {
    
      [listBox setDataSource:self];
      [listBox sizeLastColumnToFit];
    
    }
    
    - (int)numberOfRowsInTableView: (NSTableView *)theTableView {
    
      return [listElements count];
      
    }
    
    - (id)tableView: (NSTableView *)theTableView 
            objectValueForTableColumn: (NSTableColumn *)theColumn 
            row: (int)rowIndex {
            
      return [listElements objectAtIndex:rowIndex];
        
    }
    
    - (id)init {
    
      int a, b, c;
      NSString *foo;
    
      [super init];
      
      listElements = [[NSMutableArray alloc] initWithCapacity:10];
      
      for (a = 1; a <= 26; a++) {
    
        for (b = 1; b <= 26; b++) {
    
          for (c = 1; c <= 26; c++) {
          
            foo = [NSString stringWithFormat:@"%c%c%c", a + 96, b + 96, c + 96];
            
            [listElements addObject:foo];
    
          }
    
        }
    
      }
    
      return self;
      
    }
    
    - (void)dealloc {
    
      [listElements release];
    
      [super dealloc];
      
    }
    
    @end
    
    The "-numberOfRowsInTableView:" and "-tableView: objectValueForTableColumn: row:" methods are called by the NSTableView to find out what it should display.
     
  9. mmmdreg thread starter macrumors 65816

    mmmdreg

    Joined:
    Apr 14, 2002
    Location:
    Sydney, Australia
    #9
    damn.. no wonder VB 's for n00bs. I mean.. look at the difference.
     
  10. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #10
    I would never suggest that Visual BASIC be used for people new to programming. Borland has three products which are also off the drag-and-drop programming variety. You get a lot done, but do you really know how to programme afterwards?

    I've dealt with a lot of software that was created with VB and one thing is consistent: it's sloppy. When I worked at the hospital, I got a call for help one night. I went over there and they explained what they were doing and that the computer had just stopped. I looked around, pressed the online/ready button on the printer, and the computer started to work again. Now, if the programmer had dared to check the printer status...
     
  11. szark macrumors 68030

    szark

    Joined:
    May 14, 2002
    Location:
    Arid-Zone-A
    #11
    Actually, I think VB can be used for teaching basic programming concepts to newbies, as long as it's done correctly.

    The main problem I've seen regarding VB is that, because it's "Visual" BASIC, teachers and books try to teach visual interface concepts at the same time as programming skills, when they should be mastered separately.

    VB's fine for learning basic skills (pun intended), but after that a programmer should move to a language that has the flexibility needed for complex programs.
     

Share This Page