JSPatch項目常用代碼片段整理

在公司做的2個項目都是企業證書發佈,沒有上架App Store,也經常需要發熱修復,所以將項目中常用的代碼片段記錄起來,方便複用

1、私有變量

//_timestamp是帶下劃線的變量名
self.valueForKey("_timestamp");
self.setValue_forKey(value, "_timestamp");

2、NSString

//佔位符都用@
var state = NSString.stringWithFormat("%@", 'success');
state = NSString.stringWithFormat("%@", 1000);

//判斷字符串相等
state.isEqualToString('success');

//字符串轉整型
var value = state.integerValue();

//字符串轉浮點型
var value = state.doubleValue();

//保留2位小數
var value = NSString.stringWithFormat("%@", dict.objectForKey("sendredmoney"));
var value1 = value.integerValue();
var value2 = NSString.stringWithFormat("%@", value1.toFixed(2));

//字符串處理
var string1 = string.substringWithRange({location: 4, length: 1});
var string2 = string.substringWithRange({location: 8, length: 1});
var resultString = string.stringByReplacingCharactersInRange_withString({location: 4, length: 1}, string2);
var resultString = resultString.stringByReplacingCharactersInRange_withString({location: 8, length: 1}, string1);

//計算寬度
var width = title.boundingRectWithSize_options_attributes_context({width: 1000, height: 20}, 1, {'NSFont': UIFont.boldSystemFontOfSize(14)}, null).width;

//去除空字符、換行符
var email = email.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet());

3、NSDictionary、NSMutableDictionary

//NSDictionary
var params = {'sid': '12345678'};
var sid = resultDic.objectForKey("sid");

//NSMutableDictionary
params.setObject_forKey('12345678', "sid");

4、NSArray

//用setter方法賦值
self.setDataArray({'1', '2', '3', '4'});
//取值
self.dataArray().objectAtIndex(index);
//類方法創建
var array = NSArray.arrayWithObjects('1', '2', null);

5、MJExtension

UnderwriterModel.mj__objectArrayWithKeyValuesArray(dict.objectForKey('underwriterList'));

6、CGRect

self.scrollView().setFrame({x:0, y:0, width: SCREEN_W, height: SCREEN_H});

7、CGSize

self.scrollView().setContentSize({width: SCREEN_W, height: SCREEN_H});

8、Block

//方式一
//創建block,因JSPatch公開版本bug,回調時有1個參數,則需定義2個,2個則定義3個,以此類推
var testBlock = block('id, id', function(resultDic, empty)
{
    
});

//網絡請求,block作爲參數
EDHTTPEngine.postRequestWithParam_Url_Block_failureBlock(params, url, testBlock, null);

//方式二
//創建block
var leftButtonClickBlock = block(function(){
    
});
//用setter方法爲block賦值
view.setDidLeftButtonClickBlock(leftButtonClickBlock);

//block作爲參數,及回調
showGetEnergyAlertViewWithEnergy_complete:function (energy, complete)
{
    dispatch_after(1.0, function(){
        if(complete)complete();
    });
}

9、NSUserDefaults

var userDefaults = NSUserDefaults.standardUserDefaults();
var value = userDefaults.boolForKey("isFirstLaunchTickTock");
if (!value) 
{
    userDefaults.setBool_forKey(1, "isFirstLaunchTickTock");
}

10、NSNotificationCenter

NSNotificationCenter.defaultCenter().addObserver_selector_name_object(self, 'updateNotification', 'NOTIFICATION_UPDATE', null);
NSNotificationCenter.defaultCenter().postNotificationName_object(NOTIFICATION_UPDATE, null);

12、dispatch

dispatch_after(1.0, function(){
    self.navigationController().popViewControllerAnimated(true);
})

13、動畫

//CGAffineTransform在JSPatch中不能直接用,需要定義struct類型
require('JPEngine').defineStruct({
    "name": "CGAffineTransform",
    "types": "FFFFFF",
    "keys": ["a", "b", "c", "d", "tx", "ty"]
})

//旋轉動畫,Math.cos()等爲JS函數
UIView.beginAnimations_context(null, null);
if (orientation == 4)
{
    var transform = {a: Math.cos(Math.PI/2), b: Math.sin(Math.PI/2), c: -Math.sin(Math.PI/2), d: Math.cos(Math.PI/2), tx: 0, ty: 0};
    self.setTransform(transform);
}
else
{
    var transform = {a: Math.cos(-Math.PI/2), b: Math.sin(-Math.PI/2), c: -Math.sin(-Math.PI/2), d: Math.cos(-Math.PI/2), tx: 0, ty: 0};
    self.setTransform(transform);
}
UIView.setAnimationDuration(1.0);
UIView.commitAnimations();

14、定義常用方法

/******************************************************************************************************************/
/************************************************  General method  ************************************************/
/******************************************************************************************************************/

//國際化
function localizedString(string) {
    return EDHotFixUtil.localizedString(string);
}

//16進制顏色
function hexColor(string) {
    return EDTools.colorWithHexString_alpha(string, 1);
}

//打印
function debugLog(sender) {
    EDHotFixUtil.log(sender);
}

//包名
function bundleIdentifier() {
    return NSBundle.mainBundle().bundleIdentifier();
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章