What is a .h file in Objective-C?

← Objective-C answers

A .h header file declares a class's public interface, which other files import. The implementation lives in the matching .m file.

// Greeter.h: the public interface
#import <Foundation/Foundation.h>
 
NS_ASSUME_NONNULL_BEGIN
 
@interface Greeter : NSObject
@property (nonatomic, copy) NSString *name;
- (NSString *)greeting;
@end
 
NS_ASSUME_NONNULL_END
// Greeter.m: the implementation
#import "Greeter.h"
 
@implementation Greeter
- (NSString *)greeting {
    return [NSString stringWithFormat:@"Hello, %@", self.name];
}
@end
  • #import includes a header once, unlike #include, which can include it repeatedly.
  • Keep headers small: forward-declare classes with @class and import the full header only in the .m file.
  • Anything you declare in a .h is visible to every file that imports it, so put private properties in a class extension inside the .m.
  • In mixed projects, a bridging header (a .h file) is how Swift sees your Objective-C code.

Related questions

Start debugging smarter today

Try Bugsee for free — no commitments, no hidden fees.

Free sign upLive demoNo credit card required