将包含 NSDictionary 对象的 NSArray 传递给 Javascript 函数
我有一个要求,其中包含 NSDictionary 对象的 NSArray 中有大量信息。现在我必须通过 webview 将它们传递到 javascript 函数中,使用:
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script
我的问题是:
- 如何在调用者端准备我的参数以便 Javascript 理解它?
- 我如何在 Javascript 中接收它们?基本上,函数签名应该是什么以及如何读取java函数内部数组的字典对象的键值对?
我对Javascript基本上没有足够的经验,所以如果这个问题太抽象请原谅我,但我希望我的问题很清楚!
编辑:我来到跨线程< /a> 表明将 NSDictionary 对象传递给 javascript 函数是不可能的。那么,我们怎样才能实现呢?
谢谢&问候, 拉吉
I have a requirement wherein I have huge set of information in a NSArray which contains NSDictionary objects. Now I have to pass them into a javascript function through a webview using:
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script
My questions are:
- How do I prepare my arguments at the caller end so that Javascript understands it?
- How do I receive them in Javascript? Basically, what should be the function signature and how do I read the key-value pairs of dictionary objects of the array inside java function?
I basically do not have enough experience when it comes to Javascript, so forgive me if this question is too abstract, I hope that my question is clear though!
Edit: I came across a thread which states that passing NSDictionary objects to javascript functions is not possible. So, how can we achieve it?
Thanks & Regards,
Raj
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于您只能向
-stringByEvaluatingJavaScriptFromString:
提供字符串,因此您需要序列化您的参数。最简单的方法是使用各种 Cocoa JSON 库之一将其转换为 JSON。然后可以将其作为 JavaScript 文本插入到您的代码中。类似于:在 JSON 中,NSDictionaries 将表示为 JavaScript 对象,因此您可以通过
array[5].someKey
(或array[5]["some key “]
)。Since you can only provide a string to
-stringByEvaluatingJavaScriptFromString:
, you need to serialize your parameters. The simplest way would be to convert it to JSON using one of the various Cocoa JSON libraries. This can then be inserted in your code as a JavaScript literal. Something like:In the JSON, the NSDictionaries will be represented as JavaScript Objects, so you’d access their members as
array[5].someKey
(orarray[5]["some key"]
).你也可以把它们撕开,然后把它们串起来,看起来这家伙就是正在做....
You could also rip'em'apart and string 'em back together, like it looks this guys is doing....
我现在很多聪明了...☺并且我认为 - 可能,也许...这个(GitHub 上的 JSONKit) 是一个更好的解决方案...... JSONKit 提供了映射到以下 Objective-C Foundation 类的以下原始类型:
对象是
关联数组
、键/值哈希表
、Maps
或Dictionaries
等。JSONKit在内部使用 Core Foundation,并且假设对于每个等效的基本类型,Core Foundation == Foundation,即CFString == NSString。另请注意,他们说它不是 ARC 安全的。
I'm a LOT smarter now... ☺ and I think - possibly, maybe... that this (JSONKit on GitHub) is a better solution.... JSONKit provides the following primitive types mapped to the following Objective-C Foundation classes:
Objects are
Associative Arrays
,Key / Value Hash Tables
,Maps
, orDictionaries
, etc.JSONKit uses Core Foundation, internally, and it is assumed that Core Foundation ≡ Foundation for every equivalent base type, i.e. CFString ≡ NSString. Also to note, they say it is not ARC-safe.