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

mmmdreg

macrumors 65816
Original poster
Apr 14, 2002
1,393
0
Sydney, Australia
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?

dim a, b, c as integer
dim foo as string

for a = 1 to 26
--for b = 1 to 26
----for c = 1 to 26
------foo = chr(a+96) & chr(b+96) & chr(c+96)
------listBox.additem foo
----next c
--next b
next a

The "-"'s are the indentations cos this can't recognise space's it seems.
 
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.
 
Re: Some cocoa help:p

Originally posted by mmmdreg
The "-"'s are the indentations cos this can't recognise space's it seems.

You just need to use "code" tags (the # button on the post submission page).


Here's my version:

Code:
int a, b, c;
NSString foo;

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];

      [listBox addItemWithObjectValue: foo];

    }

  }

}

There are quite a few good tutorials on the web, and Apple has quite a bit of documentation (with tutorials) included with the development tools (/Developer/Documentation/Cocoa/).
 
Originally posted by mmmdreg
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

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.
 
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.
 
Originally posted by mmmdreg
damn.. no wonder VB 's for n00bs. I mean.. look at the difference.

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...
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.