How work with class method?

Discussion in 'Mac Programming' started by M.Femi, Mar 13, 2010.

  1. M.Femi macrumors newbie

    Joined:
    Mar 13, 2010
    #1
    Hello all, i have a classical C program from two functions(main and children).
    I created Cocoa project(i creating GUI for my old console C program), what we have...
    myprog.h
    Code:
    #import <Cocoa/Cocoa.h>
    
    @interface myprog : NSObject {
        IBOutlet id int_field;
        IBOutlet id result_field;
    }
    - (IBAction)button_start:(id)sender;
    
    //insert from my old C program
    [B]+ (void) children:(char) string1[], (int) integer1 and:(int) integer2);[/B]
    
    @end
    
    myprog.m
    Code:
    @implementation myprog
    - (IBAction)button_start:(id)sender {
    	int minvalue=1;
    	int maxvalue=100;
            etc..................
    	
            //from C program function
    	[B]children(string1, integer1, integer2);[/B]
    }
    @end
    
    How to make a function "children" as class method myprog???that I could call a function "children" from main code
    - (IBAction)button_start: (id)sender

    What the correct syntax for "children" in .h and .m files???
    I need to keep the function "children" as an external function.

    Thx for advice.
     
  2. rossipoo macrumors regular

    Joined:
    Jun 7, 2009
    #2
    Are you saying you want to know how to create a class method?

    Code:
    @interface MyClass : NSObject {
       ...
    }
    + (void) classMethodWithArgument:(int)arg;
    @end
    
    
    @implementation MyObject
    + (void) classMethodWithArgument:(int)arg {
       ...
    }
    @end
    
    // the proper way to call it from within your class is using:
    [[self class] classMethodWithArgument: 5];
    // to call it from outside your class use:
    [MyClass classMethodWithArgument: 5]; 
    
    Note that Objective-C is a strict superset of C which means you can write any C code without converting it to objC. If you want to declare a C function in your header file, you must do so outside of the @interface block, it must be before the interface line, or after the @end line. Same thing in your .m file, you need to write the plain c function before the @implementation line, or after the @end line.
     
  3. M.Femi thread starter macrumors newbie

    Joined:
    Mar 13, 2010
    #3
    Correct?

    .m file
    Code:
    @implementation myprog
    + (void) func2:(char) res[], (int) min_value, (int) max_value{
          .....................
    }
    
    + (void) func1:(char) ser[], int value11{
    	....
    }
    
    - (IBAction)button_start:(id)sender {
    
    	.....
    }
    @end
    
    
    .h file
    Code:
    @interface tristscan : NSObject {
        IBOutlet id textfield1;
        IBOutlet id textfield2;
        IBOutlet id textfield3;
        ....
    }
    - (IBAction)button_start:(id)sender;
    
    + (void) func2:(char) res[], (int) min_value, (int) max_value;
    + (void) func1:(char) resci[], int value11;
    @end
    
    [​IMG]
    because of + (void) func2:(char) res[], (int) min_value, (int) max_value;
    if i change "(char) res[]" to "(char) res" this error disappears (but it's not suitable=)
    If i can't use the argument (char) res[] Is possible to replace this's string parameter for Obj-C syntax (syntax classic C is not valid).
     
  4. GorillaPaws macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #4
    You're missing some Objective-C fundamentals. You should read and understand this before moving forward. If you're still having problems, then I would recommend Kochan's "Programming in Objective-C 2.0."
     

Share This Page