# ObjCMixin
[![CI Status](http://img.shields.io/travis/huandu/ObjCMixin.svg?style=flat)](https://travis-ci.org/huandu/ObjCMixin)
[![Version](https://img.shields.io/cocoapods/v/ObjCMixin.svg?style=flat)](http://cocoapods.org/pods/ObjCMixin)
[![License](https://img.shields.io/cocoapods/l/ObjCMixin.svg?style=flat)](http://cocoapods.org/pods/ObjCMixin)
[![Platform](https://img.shields.io/cocoapods/p/ObjCMixin.svg?style=flat)](http://cocoapods.org/pods/ObjCMixin)
## Usage
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
```objc
#import <ObjCMixin/ObjCMixin.h>
```
### Define and implement a module
Declare a module.
```objc
@module(MyModule)
@property(nonatomic, strong) NSString *prefix;
- (void)saySomething:(NSString *)something;
@end
```
Implement module methods in a `.m` file.
```objc
@module_implementation(MyModule)
// 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);
}
@end
```
### Mixin an interface with module
Declare an interface.
```objc
@interface Foo : NSObject<MyModule>
- (void)foo;
@end
```
Implement Foo's method and mixin it with `MyModule` in a `.m` file.
```objc
@mixin(Foo, MyModule)
@implementation Foo
- (void)foo {
NSLog(@"Foo");
}
@end
```
### Use mixed interface
Use `Foo` to call methods in `MyModule`.
```objc
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.
## Limitation
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](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html#//apple_ref/doc/uid/TP40011210-CH6-SW1). The only difference between a module and a category is that code in module can be shared among more than one interface.
## Installation
ObjCMixin is available through [CocoaPods](http://cocoapods.org). To install
it, simply add the following line to your Podfile:
```ruby
pod "ObjCMixin", "~> 0.1.0"
```
没有合适的资源?快使用搜索试试~ 我知道了~
Object-C-MixinObject-C-MixinObject-C-Mixin
共30个文件
m:6个
h:4个
xcworkspacedata:2个
0 下载量 105 浏览量
2024-03-11
11:24:50
上传
评论
收藏 19KB RAR 举报
温馨提示
## Usage 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 ```objc #import <ObjCMixin/ObjCMixin.h> ``` ### Define and implement a module Declare a module. ```objc @module(MyModule) @property(nonatomic, strong) NSString *prefix; - (void)saySomething:(NSString *)something; @end
资源推荐
资源详情
资源评论
收起资源包目录
Object-C-Mixin.rar (30个子文件)
Object-C-Mixin
.travis.yml 569B
Pod
Assets
.gitkeep 0B
Classes
ObjCMixin.h 939B
ObjCMixin.m 1KB
LICENSE 1KB
Example
Podfile 284B
ObjCMixin.xcworkspace
contents.xcworkspacedata 227B
ObjCMixin.xcodeproj
xcshareddata
xcschemes
ObjCMixin-Example.xcscheme 4KB
project.pbxproj 28KB
project.xcworkspace
contents.xcworkspacedata 154B
ObjCMixin
Main.storyboard 2KB
Images.xcassets
AppIcon.appiconset
Contents.json 826B
LaunchImage.launchimage
Contents.json 1KB
OCMAppDelegate.h 273B
OCMAppDelegate.m 2KB
OCMViewController.h 209B
OCMViewController.m 502B
main.m 330B
ObjCMixin-Info.plist 1KB
ObjCMixin-Prefix.pch 321B
OCMTestModule.h 427B
OCMTestModule.m 1KB
en.lproj
InfoPlist.strings 45B
Tests
Tests-Prefix.pch 166B
Tests-Info.plist 698B
en.lproj
InfoPlist.strings 45B
Tests.m 1KB
Podfile.lock 383B
ObjCMixin.podspec 1015B
README.md 3KB
共 30 条
- 1
资源评论
嘻嘻爱编码
- 粉丝: 843
- 资源: 145
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功