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#importincludes a header once, unlike#include, which can include it repeatedly.- Keep headers small: forward-declare classes with
@classand import the full header only in the.mfile. - Anything you declare in a
.his 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
.hfile) is how Swift sees your Objective-C code.
