Microkid
Microkid

Reputation: 33

C# and Objective C Classes

I am just learning Objective C and was wondering what the correct way to create and use a class is.

In C# I can do something like:

Void MyMethod()
{
  MyClass clsOBJ1 = new MyClass();
  clsOBJ1.Name = “Fred”;
}

Or even

MyClass clsOBJ2;
Void MyMethod()
{
 clsOBJ2 = new MyClass();
}

Void MyMethod2()
{
 clsOBJ2.Name = “Bob”;
}

How would you achieve something similar in OBJ C?

I have tried this in OBJ C:

MyClass clsOBJ3 = [[[MyClass alloc]init]autorelease];

But I get the error message ‘MyClass Not Declared’

Thanks :-)

Upvotes: 1

Views: 97

Answers (2)

Perception
Perception

Reputation: 80603

You would typically have the following:

MyClass.h

@interface MyClass : NSObject {
@private
   // your ivars here
}

// your property declarations and methods here
@end

MyClass.m

#import "MyClass.h"

@implementation MyClass

// synthesize your properties here

- (id) init {
    if((self = [super init]) != null) {
        // some init stuff here
    }

    return self;
}

- (void) dealloc {
    // your class specific dealloc stuff
    [super dealloc];
}

And in some other file you could then use MyClass as so:

SomeOtherFile.m

#import "MyClass.h"

- (MyClass *) createAMyClass {

    MyClass * mClass = [[MyClass alloc] init];

    return [mClass autorelease];
}

Upvotes: 1

jaywayco
jaywayco

Reputation: 6296

I need to see more of your code to be sure but my guess would be that you havent imported the header for MyClass

At the top of your file look for:

#import "MyClass.h"

Or something similar

Upvotes: 2

Related Questions