Latest 0.1.1
License MIT

CI Status


To run the example project, clone the repo, and run pod install from the Example directory first.

There is a test case in Example/Tests/Tests.m. It’s a good start to understand how to write mixin in Object-C.

Import ObjCMixin

#import <ObjCMixin/ObjCMixin.h>

Define and implement a module

Declare a module.

@property(nonatomic, strong) NSString *prefix;
- (void)saySomething:(NSString *)something;

Implement module methods in a .m file.


// Module must implement methods required by property explicitly.
- (NSString *)prefix {
  // Use `module_ivar` to retrieve a variable in module.
  return module_ivar(self, prefix);

- (void)setPrefix:(NSString *)prefix {
  // Update module instance variable.
  module_ivar(self, prefix) = prefix;

// Implement method as usual.
- (void)saySomething:(NSString *)something {
  NSString *prefix = _prefix;
  NSLog(@"%@: %@", prefix, something);


Mixin an interface with module

Declare an interface.

@interface Foo : NSObject<MyModule>
- (void)foo;

Implement Foo’s method and mixin it with MyModule in a .m file.

@mixin(Foo, MyModule)

@implementation Foo

- (void)foo {


Use mixed interface

Use Foo to call methods in MyModule.

Foo *f = [[Foo alloc] init];
[f hello];
[f foo];
f.prefix = @"Hey";
NSLog(@"%@, you!", f.prefix);

Helper macros

ObjCMixin provides following macros to make mixin cool.

  • @module(ModuleName): Declare a module.
  • @module_implementation(ModuleName): Implement module methods.
  • @mixin(TargetClass, ModuleName): Copy all methods from module to TargetClass.
  • module_ivar(self, ivar): Get or set a variable binded with module instance.


ObjCMixin uses Object-C runtime api to hack classes. Due to runtime api limitations, ObjCMixin can only copy module methods to target class. Instance variables and synthesized properties cannot be copied from module class to target class.

Basically, a module can be considered as a category. The only difference between a module and a category is that code in module can be shared among more than one interface.


ObjCMixin is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "ObjCMixin", "~> 0.1.0"


Huan Du, [email protected]


ObjCMixin is available under the MIT license. See the LICENSE file for more info.

Latest podspec

    "name": "ObjCMixin",
    "version": "0.1.1",
    "summary": "Add @module and @mixin to Object-C.",
    "description": "                       This pod provides several macros to implement mixins in Object-C.n",
    "homepage": "",
    "license": "MIT",
    "authors": {
        "Huan Du": "[email protected]"
    "source": {
        "git": "",
        "tag": "0.1.1"
    "requires_arc": false,
    "source_files": "Pod/Classes/**/*",
    "public_header_files": "Pod/Classes/ObjCMixin.h"

Pin It on Pinterest

Share This