PhoneGap.exec() 在 JS 和 Obj-C 之间传递对象

发布于 2024-09-28 16:57:30 字数 482 浏览 5 评论 0原文

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

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

对象:

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

...并在 Obj-C 中重建 那正确吗?有更好/正确/官方的方法来做到这一点吗?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

(り薆情海 2024-10-05 16:57:30

PhoneGap.exec 是为简单类型设计的。您的方法没问题,或者您可以只传递单个对象(仅适用于单个对象,请参阅页脚了解我们如何编组命令),并且它应该位于命令的选项字典中。然后在 Objective-C 端,使用键值编码自动用字典填充您的自定义对象。

例如
MyCustomObject* blah = [MyCustomObject new];
[blah setValuesForKeysWithDictionary:options];

如果您对 PhoneGap.exec 的工作原理感兴趣,请继续阅读...

* --------- *

对于 PhoneGap.exec,JavaScript 参数被编组到 URL 中。

对于 JS 命令:
PhoneGap.exec('MyPlugin.command', 'foo', 'bar', 'baz', { mykey1: 'myvalue1', mykey2: 'myvalue2' });

生成的命令 URL 为:
gap://MyPlugin.myCommand/foo/bar/baz/?mykey1=myvalue1&mykey2=myvalue2

这将在 Objective-C 端处理和转换。 foo、bar、baz 放在arguments 数组中,查询参数放在options 字典中。它将查找名为“MyPlugin”的类,并使用参数数组和选项字典作为参数来调用选择器“myCommand”。

有关更多信息,请参阅phonegap.js,查看PhoneGap.run_command

PhoneGap.exec was designed for simple types. Your way is ok, alternately you can just pass your single object in (would only work for a single object only, see footer about how we marshal the command), and it should be in the options dictionary for the command. Then on the Objective-C side, use key-value coding to automatically populate your custom object with the dictionary.

e.g.
MyCustomObject* blah = [MyCustomObject new];
[blah setValuesForKeysWithDictionary:options];

If you are interested in how PhoneGap.exec works, read on...

* --------- *

For PhoneGap.exec, the javascript arguments are marshaled into a URL.

For the JS command:
PhoneGap.exec('MyPlugin.command', 'foo', 'bar', 'baz', { mykey1: 'myvalue1', mykey2: 'myvalue2' });

The resulting command url is:
gap://MyPlugin.myCommand/foo/bar/baz/?mykey1=myvalue1&mykey2=myvalue2

This will be handled and converted on the Objective-C side. foo, bar, baz are put in the arguments array, and the query parameters are put in the options dictionary. It will look for a class called 'MyPlugin' and will call the selector 'myCommand' with the arguments array and options dictionary as parameters.

For further info, see phonegap.js, look at PhoneGap.run_command

﹎☆浅夏丿初晴 2024-10-05 16:57:30

我认为这是最好的方法,即使不是唯一的方法。

PhoneGap.exec 调用只是在幕后获取对象的 NSDictionary,所以我没有看到更好的方法来处理它。

大多数方法的结构如下

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

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 {
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文