使用JSPatch的注意事项

根据苹果审核的要求,请大家暂时不要使用JSPatch了,建议已经集成了的童鞋,在下次提交的时候删除相关内容。

RT,这篇文章讲的是在使用JSPatch的过程中的注意事项,至于JSPatch的集成和基础使用说明请移步。
下面针对在使用JSPatch过程中的一些细节做出说明,有些内容在基础使用说明里面可以查阅到。

OC到JS代码的转换

  1. 推荐使用JSPatch官方提供的代码转换工具或者直接下载这个工具
  2. 通过使用这个工具,大体的代码就可以转变过来了。之后我们需要手动修改一些地方:
  • 枚举。OC代码里面的枚举统一改成对应的常量。
oc
typedef NS_ENUM(NSInteger, xxx)
{
  a = 1,
  b
}
switch(x) 
{
     case a:
     break;
     case b:
     break
}
js
switch(x) {
  case 1:
  break;
  case 2:
  break;
}
  • 布尔变量。统一使用0或1代替OC中的bool变量
  • 宏或静态常量。使用字面值代替。
oc
#define CELLID @"cellid"
[tableView dequeueReusableCellWithIdentifier:CELLID];
js
[tableView dequeueReusableCellWithIdentifier:@"cellid"];
  • 数组长度。在main.js中获取数组长度
array.count()
self.array().count()
  • 遍历数组
不支持语法糖。请使用objectAtIndex
  • 打印。
console.log()

下面给出一个事例

OC

static NSString *kCellID = @"cellid";
#define UIColorFromHexAndAlpha(s, a) [UIColor colorWithHexString:@#s alpha:a]
#define UIColorFromHex(s) [UIColor colorWithHexString:@#s]

typedef NS_ENUM(NSInteger, TestStatus)
{
    TestStatusComplete,// 已结束
    TestStatusPrepare, // 未开始
    TestStatusDoing,   // 进行中
};

typedef NS_ENUM(NSInteger, TestType)
{
    TestTypeSelectFirst = 1,    
    TestTypeSecond               
};

@implementation TestViewController

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TestModel *model;
    if (indexPath.row < self.dataSource.count)
    {
        model = self.dataSource[indexPath.row];
    }
    
    TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
    
    if (model == nil)
    {
        return cell;
    }
    
    cell.titleLabel.text = model.explain;
    
    // 直播活动不显示选角信息
    // 如果参与了选角显示自己获得的票数,否则显示参与活动的关注的主播获得的票数
    if (model.type == TestTypeSelectFirst)
    {
        cell.firstButton.hidden = NO;
        cell.bottomGrayView.hidden = NO;
        if (model.name.length > 0)
        {
            [cell.firstButton setAttributedTitle:model.firstInfo forState:UIControlStateNormal];
        }
        else
        {
            [cell.fistButton setAttributedTitle:model.secondInfo forState:UIControlStateNormal];
        }
    }
    else
    {
        cell.votesButton.hidden = YES;
        cell.bottomGrayView.hidden = YES;
    }
    [cell.coverImageView imageWithUrlStr:model.pic placeholderImageStr:kDefaultStudio];
    
    NSString *stateStr;
    UIColor *stateBg;
    UIColor *textColor = [UIColor whiteColor];
    cell.stateLabel.hidden = NO;
    switch (model.status)
    {
        case TestStatusPrepare:
        {
            stateStr = @"准备中";
            stateBg = UIColorFromHexAndAlpha(000000, 0.4);
            break;
        }
        case TestStatusDoing:
        {
            stateStr = @"进行中";
            stateBg = UIColorFromHexAndAlpha(FFDE0A, 1.0);
            textColor = UIColorFromHex(333333);
            break;
        }
        case TestStatusComplete:
        {
            stateStr = @"已结束";
            stateBg = UIColorFromHex(A0A0A0);
            break;
        }
        default:
            cell.stateLabel.hidden = YES;
            break;
    }
    cell.stateLabel.text = stateStr;
    cell.stateLabel.backgroundColor = stateBg;
    cell.stateLabel.textColor = textColor;
    cell.sponsorPhoto = model.photo;
    cell.sponsorNickname = model.nickName;
    return cell;
}

JS

require('UIColor');
defineClass('TestViewController', {
        tableView_cellForRowAtIndexPath: function(tableView, indexPath) {
        
        var model;
        var row = indexPath.row();
        if (row < self.dataSource().count()) {
            model = self.dataSource().objectAtIndex(indexPath.row());
        }
        
        var cell = tableView.dequeueReusableCellWithIdentifier("cellid");
        
        if (!cell) {
            cell = YZJStudioTableViewCell.alloc().initWithStyle_reuseIdentifier(UITableViewCellSelectionStyleDefault, "cellid");
        }
            
        if (!model) {
            return cell;
        }
        cell.titleLabel().setText(model.explain());
        // 直播活动不显示选角信息
        // 如果参与了选角显示自己获得的票数,否则显示参与活动的关注的主播获得的票数
        if (model.type() == 1) {
            cell.firstButton().setHidden(0);
            cell.bottomGrayView().setHidden(0);
        if (model.roleName().length() > 0) {
            cell.firstButton().setAttributedTitle_forState(model.firstInfo(), 0);
        } else {
            cell.votesButton().setAttributedTitle_forState(model.secondInfo(), 0);
        }
        } else {
            cell.firstButton().setHidden(1);
            cell.bottomGrayView().setHidden(1);
        }
        cell.coverImageView().imageWithUrlStr_placeholderImageStr(model.pic(), "defult_pic");
        var stateStr;
        var stateBg;
        var textColor = UIColor.whiteColor();
        cell.stateLabel().setHidden(0);
        switch (model.status()) {
        case 1:
        {
            stateStr = "准备中";
            stateBg = UIColor.colorWithHexString_alpha("#000000", 0.4);;
        break;
        }
        case 2:
        {
            stateStr = "进行中";
            stateBg = UIColor.colorWithHexString_alpha("#FFDE0A", 1.0);
            textColor = UIColor.colorWithHexString("#333333");;
            break;
        }
        case 0:
        {
            stateStr = "已结束";
            stateBg = UIColor.colorWithHexString("#A0A0A0");;
            break;
        }
        default:
            cell.stateLabel().setHidden(1);
            break;
        }
        cell.stateLabel().setText(stateStr);
        cell.stateLabel().setBackgroundColor(stateBg);
        cell.stateLabel().setTextColor(textColor);
        cell.setSponsorPhoto(model.photo());
        cell.setSponsorNickname(model.nickName());
        return cell;
        },
});

你可能感兴趣的:(使用JSPatch的注意事项)