Skip to content

December 11, 2011

Defining a Class in Objective C

by noise

To define a class in Objective C you place the @interface part in .h file and the @implementation part in .m file.

Example 1. Class definition in Objective C

To define class MyNotes use the following code for definition file (.h):

// MyNotes.h
@interface MyNotes : NSObject
@end

And the implementation file (.m):

// MyNotes.m
@implementation MyNotes
@end

Then we define a @property which help use creating setters and getters. The @property part is added to our definition file and the @synthesize is added to implementation file.

Example 2: @property usage

// MyNotes.h
@interface MyNotes : NSObject
@property (retain) NSString *note;
@end

And the implementation file (.m):

// MyNotes.m
@implementation MyNotes
@synthesize note;
@end

Previous example is equivalent with the following code, that do not use @properties.

Example 3. Defining a class without @properties

// MyNotes.h
#import <Foundation/Foundation.h>
 
@interface MyNotes : NSObject {
    NSString *note;
}
 
- (NSString *) note;
- (void) setNote:(NSString *) val;
 
@end

And the implementation file (.m):

// MyNotes.m
#import "MyNotes.h"
 
@implementation MyNotes
 
- (NSString *) note
{
    return note;
}
 
- (void) setNote:(NSString *) val
{
    if (![val isEqualToString:nil]) {
        //note = [note stringByAppendingString:val];
        note = [NSString stringWithFormat:@"%@", val];
 
    }
    else
        NSLog(@"Error!");
}
@end

Then our main.m file will be:

// main.m
#import <Foundation/Foundation.h>
#import "MyNotes.h"
 
int main (int argc, const char * argv[])
{
 
    @autoreleasepool {
 
        MyNotes *anote = [[MyNotes alloc] init];
        anote.note = @"This is a note";
        NSLog(@"Note: %@", [anote note]);
    }
    return 0;
}

Example 4. Defining a Class with private @property

We do that by adding an @interface section in our .m (right, it’s .m file!) file and in that section we define a @property which will synthesize it in @implementation part.

See next code. Only MyNotes.m file is shown, the other two files, main.m and MyNotes.h file are the same as in previous example.

// MyNotes.m
#import "MyNotes.h"
 
// This is a private @interface since is defined in .m file
// This is called a Class Extension
@interface MyNotes()
@property (nonatomic) BOOL readOnlyNote;
@end
 
@implementation MyNotes
 
@synthesize readOnlyNote;
 
- (NSString *) note
{
    return note;
}
 
- (void) setNote:(NSString *) val
{
    if (![val isEqualToString:nil]) {
        //note = [note stringByAppendingString:val];
        note = [NSString stringWithFormat:@"%@", val];
 
    }
    else
        NSLog(@"Error!");
}
 
@end

Example 5. Using public and private @properties

Finally, a code with @public and @private properties will look line next example (we will display only MyNotes.h and MyNotes.m since main.m is the same as in example 3:

// MyNotes.h
#import <Foundation/Foundation.h>
 
@interface MyNotes : NSObject {
    NSString *note;
}
 
@property (retain) NSString *note;
 
@end

And our implementation file is:

// MyNotes.m
#import "MyNotes.h"
 
@interface MyNotes()
@property (nonatomic) BOOL readOnlyNote;
@end
 
@implementation MyNotes
 
@synthesize note;
@synthesize readOnlyNote;
 
@end
Read more from Cocoa

Leave a Reply

required
required

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments