在 iPhone 上运行时解释 Objective C 脚本?

发布于 2024-08-30 07:14:23 字数 1106 浏览 8 评论 0原文

是否有办法在运行时加载目标 c 脚本,并针对当前 iPhone 应用程序中的类/方法/对象/函数运行它?

主要注意事项: 我这样做的主要原因是为了让我能够快速构建应用程序原型,然后在完成主要原型设计阶段后,获取我正在编写的脚本并在以下位置编译它们:相反,构建时间。我从来没有计划发布带有 Objective C 解释器的应用程序。

我问的原因是我一直在玩 iPhonewax,一个可以嵌入 iPhone 应用程序中的 lua 解释器,它工作得非常好,从某种意义上说,任何在 Objective C 中公开可用的对象/方法/函数代码自动桥接,并且在 lua 中可用。

这使您可以通过简单地将应用程序的核心设为用户文档目录中的 lua 文件来快速构建应用程序原型。只需重新加载应用程序,您就可以测试对 lua 文件的更改,而无需在 XCode 中重建应用程序 - 节省大量时间!

但是,随着 Apple 最近的 3.1.3 SDK 的推出,我开始思考,进行此类快速原型设计的最安全方法是使用 Objective C 作为解释代码……这样,在最坏的情况下,您可以只需在发布之前将其编译到您的应用程序中即可。我听说 lua 源代码可以编译为字节代码,并在构建时链接,但我认为最终安全的事情是脚本源代码位于 Objective C 中,而不是 lua 中。这样,无论如何,你的源代码始终位于 Objective C 中。

这让我想知道(我已经搜索过,但一无所获)是否有任何关于如何在 iPhone 应用程序中嵌入 Objective C 解释器的示例?这将允许您根据二进制文件中内置的当前类快速构建应用程序原型,并且当您要部署应用程序时,您可以编译它们,而不是通过应用程序内解释器运行这些类。

借助 iPad 和 OS 4,蓝牙和虚拟键盘可以与 iPhone 和 iPad 配合使用……这将使这种类型的快速原型设计变得更加有用,至少对于开发时间来说是这样。例如,如果您的应用程序中内置了解释器并将其安装在 iPad 上,则您可以在路上针对解释器进行编码,而无需使用 XCode。对我来说,让源代码恢复到“苹果批准”状态的最有用的方法是脚本是 Objective C。

Is there anyway to load an objective c script at runtime, and run it against the classes/methods/objects/functions in the current iPhone app?

MAJOR NOTE: The major reason I'd like to do this is to allow me to rapidly prototype an application, and then, after I'm done a major prototyping phase, take the scripts I was writing and compile them at build time instead. I don't ever plan on shipping an app with an objective c interpreter in it.

The reason i ask is that I've been playing around with iPhone wax, a lua interpreter that can be embedded in an iPhone app, and it works very nicely, in the sense that any object/method/function that's publically available in your Objective C code is automatically bridged, and available in lua.

This allows you to rapidly prototype applications by simply making the core of your app be lua files that are in the users documents directory. Just reload the app, and you can test out changes to your lua files without needing to rebuild the app in XCode - a big time saver!

But, with Apples recent 3.1.3 SDK stuff, it got me thinking that the safest approach for doing this type of rapid prototypeing would be if you could use Objective C as the interpreted code... That way, worst case scenario, you could just compile it into your app before your release instead. I have heard that the lua source can be compiled to byte code, and linked in at build time, but I think the ultimate safe thing would be if the scripted source was in objective c, not lua. That way your source is always in objective c, regardless.

This leads me to wondering (i've searched, but come up with nothing) if there are any examples on how to embed an Objective C Interpreter in an iPhone app? This would allow you to rapidly prototype your app against the current classes that are built into your binary, and, when your about to deploy your app, instead of running the classes through the in app interpreter, you compile them in instead.

With the iPad and OS 4, bluetooth and virtual keyboards can work with iPhones and iPads... That's going to make this type of rapid prototyping something much more useful, at least for dev time. If you have an interpreter built into your app and have it on your iPad, for example, you can code against the interpreter while on the road, without XCode. And to me, the most useful way to get the source back to an "apple approved" state would be if the scripts were Objective C.

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

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

发布评论

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

评论(8

野却迷人 2024-09-06 07:14:23

Objective-C 实际上只是带有运行时和一些语法糖的 C。它本质上是一种编译语言(我认为 C 语言没有任何可用于生产的解释器,尽管我可能是错的)。

Xcode 曾经有一个名为 ZeroLink 的功能来加快编译时间,但在Xcode 3 因为它造成了太多的错误。

Objective-C is really just C with a runtime and some syntactic sugar. It's an inherently compiled language (I don't think there are any production-ready interpreters for C, although I might be wrong).

Xcode used to have a feature called ZeroLink to speed up compile time, but removed it in Xcode 3 because it caused too many bugs.

无法回应 2024-09-06 07:14:23

这并非完全不可能,但并不容易,不值得这样做。 Objective-C 通常不是解释性语言。这并不是不可克服的——在大多数情况下,解释与编译只是一种实现选择。例如,Ruby 传统上被认为是一种解释性脚本语言,但 MacRuby 将其编译为类似于 Objective-C 生成的代码。因此,为 Objective-C 编写一个解释器是可能的,但还没有人这样做过。你必须自己写。

此外,规则禁止 Apple 的 Javascript 解释器以外的解释器。到目前为止,这还没有对任何人强制执行,但如果你想成为一个非常直的箭头,不幸的是解释代码也被淘汰了。

It's not exactly impossible, but it wouldn't be easy enough to be worth it. Objective-C isn't normally an interpreted language. This isn't insurmountable — interpreted vs. compiled is just an implementation choice in most cases. For example, Ruby is traditionally considered an interpreted scripting language, but MacRuby compiles it down to code just like Objective-C produces. So it would be possible to write an interpreter for Objective-C, but nobody has done this. You would have to write it yourself.

Also, the rules forbid interpreters other than Apple's Javascript interpreter. So far this hasn't been enforced on anyone, but if you're trying to be a very straight arrow, interpreted code is unfortunately out as well.

黒涩兲箜 2024-09-06 07:14:23

嗯,有几个值得提出的观点:

  1. 当你可以编译 Objective-C 代码时,为什么还要解释它呢?我理解“快速原型设计”的想法,但在例如 Lua 中这样做的部分原因是因为 Lua 是一种比 Objective-C 更简洁的语言。我不知道解释 Objective-C 是否会有同样多的好处。
  2. 如果您想在应用程序中包含插件或可动态加载的模块,您始终可以使用 NSBundle 或类似机制将它们编译为单独的包并加载它们。
  3. 尽管如此,我不知道有任何 Objective-C 解释器。您可能必须自己编写。我不确定它是否会违反 Apple 的指导方针:它仍然是 Objective-C 代码,但我认为他们也有针对解释代码的规则。 (不过,我想他们从未设想过解释 Objective-C 的假设场景。)

Well, there's a couple worthwhile points to bring up:

  1. Why interpret Objective-C code when you can compile it? I understand the "rapid prototyping" idea, but part of the reason to do that in, e.g., Lua, is because Lua is a much terser language than Objective-C. I don't know if interpreting Objective-C will have as much of a bonus.
  2. If you want to have plugins or dynamically-loadable modules in your app, you can always compile them as a separate bundle and load them, using NSBundle or a similar mechanism.
  3. All that said, I don't know of any Objective-C interpreters. You'd likely have to write your own. I'm not sure if it would violate Apple's guidelines or not: it'd still be Objective-C code, but I thought they had rules against interpreted code, too. (I suppose they never envisioned a hypothetical scenario in which Objective-C was interpreted, though.)
Spring初心 2024-09-06 07:14:23

Ch 是一个商业 C/C++ 解释器。它是由一家名为 SoftIntegration 的公司制造的。

Ch is a commercial C/C++ interpreter. It's made by a company called SoftIntegration.

迷鸟归林 2024-09-06 07:14:23

不是在 iPhone 上,而是在模拟器上,您可以使用
动态代码注入工具
http://dyci.github.com/

还有另一个工具,其工作方式略有不同,但允许相同的功能
http://injectionforxcode.com/

Not on IPhone, but on Simulator, you can do this with
Dynamic Code Injection Tool
http://dyci.github.com/

There's also another tool, that works a little different way, but allows same functionality
http://injectionforxcode.com/

陌生 2024-09-06 07:14:23

您应该看看 cycript。您可以连接到应用程序、即时替换方法、更改变量,等等。它是 Objective-C 和 JavaScript 之间的混合语言。
您需要越狱您的 iDevice 才能安装它。

You should take a look at cycript. You can hook into apps, replace methods on the fly, change variables, you-name-it. It's an hybrid language between Objective-C and JavaScript.
You'll need to jailbreak your iDevice to install it.

┊风居住的梦幻卍 2024-09-06 07:14:23

查看 objc_msgSend() 的文档以及 Objective-C 运行时参考。您基本上可以解析文本并将其发送到运行时。

Take a look at the documentation for objc_msgSend() and other parts of the Objective-C Runtime Reference. You can essentially parse text and send it to the runtime.

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