添加 struct 类型支持
2016-03-03 13:27
225 查看
JSPatch 默认支持的 struct 类型只有 CGRect / CGPoint / CGSize / NSRange,若要让 JS 脚本支持其他
struct 类型,需要先手动注册。例如要支持 CGAffineTransform,需要在使用前在 JS 使用
JS 支持新的 struct 类型:
types 字段表示 struct 各字段的类型,每种类型都由一个字母代替,字母含义如下:
这里 struct 有多少个字段,types 就有多少个字母,types 的字母顺序必须按 struct 定义的顺序写,举例:
type 就是
keys 数组表示 struct 各个字段在 JS 中使用的名字,其顺序必须与 struct 字段顺序一致,拿上述
keys 数组应该是:
OC 里有
想在 JS 上使用,需要事先调用定义:
接着就可以在 JS 里使用了:
我们也可以在 JS 里动态定义 struct 类型:
struct 类型,需要先手动注册。例如要支持 CGAffineTransform,需要在使用前在 JS 使用
defineStruct()接口定义:
require('JPEngine').defineStruct({ "name": "CGAffineTransform", "types": "FFFFFF", "keys": ["a", "b", "c", "d", "tx", "ty"] })
API
JPEngine的
+defineStruct:接口,可以让
JS 支持新的 struct 类型:
@interface JPEngine : NSObject /* @param defineDict = @{ @"name": @"", //struct 名 @"types": @"", //struct 各字段类型 @"keys": @[@"", ...] //struct 各字段名 } */ + (void)defineStruct:(NSDictionary *)defineDict; ... @end
types
types 字段表示 struct 各字段的类型,每种类型都由一个字母代替,字母含义如下:'c': char 'C': unsigned char 's': short 'S': unsigned short 'i': int 'I': unsigned int 'l': long 'L': unsigned long 'q': long long 'Q': unsigned long long 'f': float 'F': CGFloat 'N': NSInteger 'U': NSUInteger 'd': double 'B': BOOL
这里 struct 有多少个字段,types 就有多少个字母,types 的字母顺序必须按 struct 定义的顺序写,举例:
struct JPDemoStruct { CGFloat a; long b; double c; BOOL d; }
JPDemoStruct对应的
type 就是
@"FldB"。
keys
keys 数组表示 struct 各个字段在 JS 中使用的名字,其顺序必须与 struct 字段顺序一致,拿上述 JPDemoStruct举例,它对应的
keys 数组应该是:
@[@"a", @"b", @"c", @"d"].
完整例子
OC 里有 JPDemoStruct这个 struct 类型:
struct JPDemoStruct { CGFloat a; long b; double c; BOOL d; }
想在 JS 上使用,需要事先调用定义:
[JPEngine defineStruct:@{ @"name": @"JPDemoStruct", @"types": @"FldB", @"keys": @[@"a", @"b", @"c", @"d"] }]
接着就可以在 JS 里使用了:
//OC @implementation JPObject + (void)passStruct:(JPDemoStruct)s; + (JPDemoStruct)returnStruct; @end
//JS require('JPObject').passStruct({a:1, b:2, c:4.2, d:1}) var s = require('JPObject').returnStruct();
动态定义
我们也可以在 JS 里动态定义 struct 类型:require('JPEngine').defineStruct({ "name": "JPDemoStruct", "types": "FldB", "keys": ["a", "b", "c", "d"] })
相关文章推荐
- 前端 网页设计之颜色对照表
- Codeforces 629C Famil Door and Brackets
- Codeforces 629C Famil Door and Brackets
- Android ImageView 的scaleType属性详解(一)
- mac上创建cocos-2d项目
- 前端,我为什么不要你
- 时间都去哪了?BPC电波授时信号的“零成本”伪造
- Python 基础关键字及符号
- 处理百万级数据提高查询速度
- cstring to utf8
- tomcat5.5 javax.servlet.ServletException: javax/el/ValueExpression
- POJ1751 Highways
- 启动Activity的两种方式startActivity和startActivityForResult(一)
- CSS书写应该注意的情况
- Linux之防火墙------查看操作
- zabbix server is not running: the information displayed may not be current
- ListView 中Item TextView 跑马灯遇到的问题
- android消息机制之二--Handler
- Android showDialog时报错requestFeature() must be called before adding content
- iOS基础--UIView的常见属性