Giter Club home page Giter Club logo

fixit's Introduction

FIXiT

CI Status Version License Platform

简介

此项目可以理解为 JS 版的 Aspect

用法示例(快速入手)

修复已有方法的 Bug

OC 中定义了类型:

@implementation NSObject (Crash)

- (void)crashIt
{
  NSLog(@"%@", @[][1]);
}

- (CGPoint)locationOf:(NSArray <NSValue *> *)locations
              atIndex:(NSInteger)index
         defaultValue:(CGPoint)point
{
  return locations[index].CGPointValue;
}

@end

添加 JS 文件以修复:

var fix = Fixit.fix('NSObject');
fix.instanceMethod('crashIt', function () {
  
});
// fix.instanceMethod 返回原实现
var originMethod = fix.instanceMethod('locationOf:atIndex:defaultValue:', function (locations, index, point) {
    // 此函数中的 this 就是 OC 的实例
    if (index < locations.length) {
        return locations[index];    
        // 或者调原实现 
        return originMethod.apply(this, arguments);
        // 等价于
        return originMethod.call(this, locations, index, point);
    }
    return point;   // 这里的 point 会变为 NSValue,直接返回即可
});

在原有实现上添加行为

已有的 OC 类定义:

@implementation MyViewController
{
    UIButton    * _button;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    ...
}

添加 JS 文件以添加行为:

require('UIAlertView, UIColor');
var fix = Fixit.fix('MyViewController');
var originViewDidLoad = fix.instanceMethod('viewDidLoad', function () {
    // 先调原实现
    originViewDidLoad.apply(this, arguments);
    
    this.view.backgroundColor = UIColor.yellowColor;
    // 或者
    this.view['setBackgroundColor:'](UIColor.yellowColor);
    
    // [] 取 JS 的属性或方法,当无参数时是属性,有参数时是方法
    var button = this['_button'];   // 与 this._button 等价
    // button 变量是 nil 安全的,当然也可以判断一下,使用 isNil 函数,
    // 而不能 if (button == nil),或者 if (button)。
    // 所有不能转为 JS 对象的 OC 对象在 JS 代码中都是一个代理对象(Proxy)
    if (!isNil(button)) {
        // 调用时参数需要与 OC 中对应
        button['setTitle:forState:']('test title', 0);
    }
    
    // 创建一个弹窗,注意使用 nil,而不是 null
    var that = this;
    dispatch_after(2, function() {
        // 注意 JS context 的变化,这里的 this 已经不是 MyViewController 了
        UIAlertView.alloc['initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:'](that.title, 'message!!!', nil, 'ok', nil).show();
    });
});

Requirements

  • Xcode 9+
  • iOS 10+

Installation

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

pod 'FIXiT'

Author

rickytan, [email protected]

License

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

fixit's People

Contributors

hztanxin avatar rickytan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

devqc

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.