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 i开发者_JAVA技巧n OBJ C:
MyClass clsOBJ3 = [[[MyClass alloc]init]autorelease];
But I get the error message ‘MyClass Not Declared’
Thanks :-)
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
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];
}
精彩评论