尝试在iPhone-Wax中创建NSDecimal [英] Trying to create NSDecimal in iPhone-Wax

查看:122
本文介绍了尝试在iPhone-Wax中创建NSDecimal的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我面临的一个问题:

Here is a problem I am facing:

我在蜡中创建一个NSDecimalNumber

I create an NSDecimalNumber in Wax with the line

    local x=NSDecimalNumber:initWithString("2.3") 

这个我想创建一个NSDecimal与以下
线

Out of this I would like to create a NSDecimal with the following line

   local y=x:decimalValue() 

这会立即导致程序崩溃。

This promptly crashes the program.

要创建相同的体验,您需要创建一个基本蜡项目,并将这两行添加为AppDelegate中 function applicationDidFinishLaunching 的斜线。 lua。

To create the same experience you need to create a basic wax project and add the two lines as the lat lines of function applicationDidFinishLaunching in AppDelegate.lua.

问题:我如何得到这个返回一个诚实的NSDecimal,我可以
传递?我实际上不需要看到或打印自己的数字。

Question: How can I get this to return an honest NSDecimal which I can pass along? I don't actually need to see or print the number myself.

附录

Appendicies


  1. 流行的信仰在互联网上,NSDecimal是非常不同的
    从NSDecimalNumber。第一个是一个C结构体,第二个是
    一个Obj-C结构体,我的工作非常好。

  2. 要让NSDecimalNumber工作,我需要注释掉从wax-helpers.m行222-224
    (NSNumber)和241-246(NSValue)。

  3. NSDecimal在Foundation / NSNumber.h中定义






运行时输出:
没有太多的堆栈跟踪,它只是默默地死了。在调试器中(打开断点),有以下(缩写)调用序列:


Output when run: There's not much of a stack trace, it just silently dies. In the Debugger (with breakpoints turned on) there is the following (abbreviated) calling sequence:

#0  0x027553f4 in objc_exception_throw
#1  0x0256e8d6 in __NSGetSizeAndAlignment
#2  0x0256ebd9 in __NSGetSizeAndAlignment
#3  0x025747b8 in __NSMS1
#4  0x02573f9c in +[NSMethodSignature signatureWithObjCTypes:]
#5  0x000342d0 in wax_selectorForInstance at wax_helpers.m:557
#6  0x00035bc2 in __index at wax_instance.m:303
#7  0x000181b9 in luaD_precall at ldo.c:319
#8  0x00018405 in luaD_call at ldo.c:376
#9  0x0002c488 in callTMres at lvm.c:88
#10 0x0002c74a in luaV_gettable at lvm.c:125
#11 0x0002dd26 in luaV_execute at lvm.c:467
#12 0x0001841c in luaD_call at ldo.c:377
#13 0x0000ddc8 in f_call at lapi.c:800
#14 0x0001758a in luaD_rawrunprotected at ldo.c:116
#15 0x0001879a in luaD_pcall at ldo.c:463
#16 0x0000de65 in lua_pcall at lapi.c:821
#17 0x00034e60 in wax_pcall at wax_helpers.m:826  
#18 0x00036be4 in pcallUserdata at wax_instance.m:580
#19 0x00036edc in wax_id_call at wax_instance.m:627

有时, trace:

wTest[36403:207] PANIC: 

Error
-----
Error calling 'applicationDidFinishLaunching:' on lua object '<AppDelegate: 0x6300e50>'
data/scripts/AppDelegate.lua:39: attempt to index local 'x' (a number value)
stack traceback:
    [C]: ?
data/scripts/AppDelegate.lua:39: in function <data/scripts/AppDelegate.lua:19>

这与调试器给出的内容匹配:Wax正在尝试调用数字值的方法。 p>

This matches what the debugger gives: Wax is trying to call a method for a number value.

推荐答案

重要编辑:好的,我很愚蠢(寻找简单问题的复杂答案)。快速阅读Wax博客,我找到了 https://github.com/probablycorey/wax/wiki / Overview ,其中提到了 toobjc 函数。因此,它看起来像... ...

Important Edit: OK, I'm stupid (and looking for complex answers to simple questions). A quick read of the Wax blog and I find https://github.com/probablycorey/wax/wiki/Overview, which mentions the toobjc function. As such, it looks a like like...

local x = (toobjc)(NDecimalNumber:initWithString("2.3"));

会做你想要的(即防止自动Lua类型转换)。我可能有语法有点错误,所以实验。

will do what you want (i.e. prevent automatic Lua type conversion). I may have the syntax slightly wrong, so experiment. I've left my original answer below to illustrate another "solution".

因此,看着蜡,不完全熟悉),桥接目标C时,它执行的一个关键行为,Lua是将所有的Objective C类型转换为合适的原生Lua类型。这包括映射到Lua数字类型的 NSNumber 类型(例如 NSDecimal )。

So, looking over Wax (which I'm not intimately familiar with), one key act it performs when bridging Objective C and Lua is to convert all Objective C types it can into suitable native Lua types. This includes NSNumber types (such as NSDecimal) which are mapped to Lua number types.

当然,你已经知道这一点,因此你对 wax-helpers.m 的修改。唉,你所做的是不够 - 转换仍在发生,因此你的 NSDecimalNumber 仍然成为一个数字。看来,根据这是在你的Lua代码中使用,Lua炸毁(试图索引一个标量类型),或Objective C桥炸毁。我不知道为什么你只得到Lua跟踪错误有时(假设你的代码总是相同);它指出一些关于违反内部蜡状态的基本假设。

Of course, you already know this, hence your changes to wax-helpers.m. Alas, what you've done isn't quite enough - conversions are still happening, and hence your NSDecimalNumber still becomes a number. It seems that, depending on whether this gets used in your Lua code, either Lua blows up (trying to index a scalar type), or the Objective C bridge blows up. I'm not sure why you only get the Lua trace error sometimes (assuming your code is always identical); it points to some underlying assumptions about the internal Wax state being violated.

最好的解决方案是不改变蜡的。目前,即使修复工作,你完全禁用类型之间的自动强制。这将,我想,打破了很多蜡码和成语。看蜡,它将只执行自动转换类型的子类的特定的基础类,它明确理解;它不知道的任何对象类型仍然是一个对象。在这种情况下,我们被捕获在 NSValue NSNumber 转换,所以我的第一个建议是简单wrap NSDecimalNumber 在一些类,蜡不明白。像... ...

The best solution will be one that doesn't involve changing Wax. At the moment, even if the "fix" did work, you've totally disabled the automated coercion between types. This will, I assume, break quite a lot of Wax code and idioms. Looking at Wax, it will only perform automated conversions for types that subclass certain Foundation classes it specifically understands; any object type that it doesn't know about remains an object. In this case, we're caught in the NSValue and NSNumber conversion, so my first suggestion is to simply wrap NSDecimalNumber in some class that Wax doesn't understand. Something like...

@interface MyDecimalWrapper : NSObject
{
  NSDecimalValue *myDecimal;
}

- (NSDecimalValue*)getDecimalValue;
- (NSDecimal*)getDecimal;

@end

@implementation MyDecimalWrapper

- (NSDecimalValue*)getDecimalValue { return [[myDecimal retain] autorelease] }
- (NSDecimal*)getDecimal { return [myDecimal decimalValue]; }

@end

这可以添加到蜡而不更改代码显着地,并使用它携带 NSDecimalValue 跨桥应该防止蜡的类型转换。当然,如果在Lua中使用 getDecimalValue ,结果将立即包装到Lua 数字中。如果你需要在Lua中的底层 NSDecimalValue 上调用方法,只需使用在包装器中定义的等效方法来代理它们。

This can be added to Wax without changing it's code significantly, and using this to carry a NSDecimalValue across the bridge should prevent Wax's type conversion. Of course, if you use getDecimalValue inside Lua, the result will promptly be wrapped into a Lua number. If you need to call methods on the underlying NSDecimalValue in Lua, just proxy them from equivalent methods defined on the wrapper.

如果这绝对不适合你,我可以做出蜡需要的变化。但它会是一个伤害维持自己的港口,并打破了大量现有蜡码和示例的世界。

If this absolutely doesn't work for you, I can probably work out the changes needed to Wax. But it will be a world of hurt maintaining your own port, and breaking lots of existing Wax code and examples.

切线:不确定你打算用一个 NSDecimal 一旦你有一个在Lua。它是一个不透明的C结构,只能通过Foundation提供的C接口使用。

Tangentially: I'm not sure what you intend to do with an NSDecimal once you've got one in Lua. It's an opaque C structure, that can only be used through a C interface provided by Foundation.

这篇关于尝试在iPhone-Wax中创建NSDecimal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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