PhoneGap.exec()在JS和Obj-C之间传递对象 [英] PhoneGap.exec() passing objects between JS and Obj-C

查看:300
本文介绍了PhoneGap.exec()在JS和Obj-C之间传递对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现在JS和Obj-C之间传递对象的唯一方法是使用JSON.stringify()对JS对象进行编码,并将json字符串传递给PhoneGap.exec

  PhoneGap.exec('Alarm.update',JSON.stringify(list)); 

...并在Obj-C中重建对象:

  NSString * jsonStr = [arguments objectAtIndex:0]; 
jsonStr = [jsonStr stringByReplacingOccurrencesOfString:@\\\withString:@\];
jsonStr = [NSString stringWithFormat:@[%@],jsonStr];
NSObject * arg = [jsonStr JSONValue];

这是正确的吗?有没有更好/正确/正式的方式这样做?

解决方案

我认为这是最好的方法,if而不是唯一的方式。



PhoneGap.exec调用只需要一个NSDictionary的对象下面,所以我没有看到一个更好的方法来处理它。 p>

大多数方法的结构类似于

   - (void)someMethod: *)参数withDict:(NSDictionary *)options {
}


The only way I found to passing objects between the JS and Obj-C it's by encoding the JS object by using JSON.stringify() and pass the json string to PhoneGap.exec

PhoneGap.exec('Alarm.update',JSON.stringify(list));

... and rebuild the object in Obj-C:

NSString *jsonStr = [arguments objectAtIndex:0];
jsonStr = [jsonStr stringByReplacingOccurrencesOfString:@"\\\"" withString:@"\""];
jsonStr = [NSString stringWithFormat:@"[%@]",jsonStr];
NSObject *arg = [jsonStr JSONValue];

It's that correct ? there is a better/proper/official way for doing this ?

解决方案

I think this is the best way to do it, if not the only way.

The PhoneGap.exec call just takes a NSDictionary of objects under the covers so I don't see of a better way to handle it.

most methods are structured like

- (void)someMethod:(NSArray*)arguments withDict:(NSDictionary*)options {
}

这篇关于PhoneGap.exec()在JS和Obj-C之间传递对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆