How to Create a Class in Objective-C

Custom classes are an essential building block in Objective-C. Ultimately, all iOS apps are made up of objects that are instantiated from classes. Here is an example of how you can code classes in Objective-C.

Objective-C Custom Class Example Header File (MyClass.h)

#import <Foundation/Foundation.h>
@interface MyClass : NSObject

@property(strong) NSString *name;

-(void)writeStateToLog;

@end

Objective-C Custom Class Example Implementation File (MyClass.m)

#import "MyClass.h"
@implementation MyClass @synthesize name;

-(void)writeStateToLog{
     NSLog(@"name = %@", self.name);
}

@end

 
Pretty simple huh?
 

5 Responses to How to Create a Class in Objective-C

  1. Summer.J September 24, 2011 at 8:19 am #

    Just came across this tutorial, I’m a newbie in iPhone app and like to try your tutorial, it will be great if you could please fix the link to the source code used in this tutorial, thank you!

    • MattjDrake September 24, 2011 at 10:41 am #

      Hi Summer,

      Got it – the source code actually should be in the blog post. But, because we’ve recently moved this blog over from another platform some of the content fell through the cracks. I’ll be putting this post back on my list to revise right now.

      • Summer September 29, 2011 at 9:01 pm #

        Thank you Matt for fixing the link. Are you planning to post the Google map example you originally posted?

        • MattjDrake September 30, 2011 at 12:15 pm #

          I don’t think so – I’m actually coming out with a really big Objective-C series that I think will be way more useful and I can’t find a copy of that code on my desktop :(

          • Jenibelle January 1, 2012 at 3:07 am #

            Me dull. You smart. That\’s just what I nedeed.

Leave a Reply

Switch to our mobile site