Tuesday, February 13, 2007

Classes in Objective-C

Something that confused me about Objective-C was how classes worked. I couldn't really find any good examples on it neither. So I sat down and played with it for a day and figured it out.

I will break down the structure for you and give you an example on how to create a working class in Objective-C.

  • In your XCode project, select File > New File
  • Scroll down to the Cocoa section and add a New Objective-C Class.
  • Change the name of the class to what ever you want, for this example, I will use ExampleClass.
  • Click create.
  • Now, in your project screen you will see two files called ExampleClass.h and ExampleClass.m
  • .h is your interface class. Providing the compiler with a brief overview of what your class has.
  • .m is your implementation class. This is where you will put your init and dealloc method as long as any methods you define in your interface class.
  • Within in your interface class (.h file) you will have the following:
@interface
{

}

@end

  • Anything placed inside the interface tag becomes attributes to the class. You can only put variables in here. Like an NSString, etc....
  • Methods are declared outside the interface brakets just right before the @end. These are just the method stubs without the { }'s

@interface
{
NSString *personName;
NSString *personAddress;
NSString *personPhone;
}
- (NSString *)PersonName;
- (NSString *)PersonAddress;
- (NSString *)PersonPhone;
- (void)setPersonName:(NSString *)value;
- (void)setPersonAddress:(NSString *)value;
- (void)setPersonPhone:(NSString *)value;

@end
  • Now to explain the code above: personName, personAddress, personPhone are only to the class. Nothing outside the class can access them. So to solve this problem and allow outside classes to communicate with these variables, you create Accessor methods. If you want a variable to be readable and writable from other classes, you need 2 methods, if you want a variable to be readable only, you need 1. Makes sense really.
  • As you can see, I didn't fill out the methods in this file. That is because it is a interface class and it can't do any work. It only tells the compiler what the class looks like.
  • To fill out the methods, you need to put them into the .m file and fill them out there.
  • In the .m file, add in the method stubs you created in the .h file.
  • After you do so, the .m file should look like this.
- (NSString *)PersonName
{
}
- (NSString *)PersonAddress
{
}
- (NSString *)PersonPhone
{
}
- (void)setPersonName:(NSString *)value
{
}
- (void)setPersonAddress:(NSString *)value
{
}
- (void)setPersonPhone:(NSString *)value
{
}
  • Now you need to fill them out. It should look like this when you are finished
- (NSString *)PersonName
{
return personName;
}
- (NSString *)PersonAddress
{
return personAddress;
}
- (NSString *)PersonPhone
{
return personPhone;
}
- (void)setPersonName:(NSString *)value
{
personName = value;
}
- (void)setPersonAddress:(NSString *)value
{
personAddress = value;
}
- (void)setPersonPhone:(NSString *)value
{
personPhone = value;
}
  • Now you need to add in your init and dealloc method. I can't remember what all is in these methods because XCode's CodeSense will plot it down for you if you type init and press return or dealloc and press return.
  • All you have to do is replace the <# Add Code Here #> tag here to have the compiler perform the the code on method when the class is loaded or thrown away.
That's it. All classes are made this way. It was confusing for me since C# classes just had a single file. Hope this helps!

No comments: