Trying to create honest NSDecimalNumber in Wax
Asked Answered
T

1

0

As I studied the issue I am facing in Trying to create NSDecimal in iPhone-Wax I am now aiming much lower. How can I create a fully functional NSDecimalNumber in Wax?

I have added the following two lines at the top of AppDelegate.lua in a fresh wax project.

local x = NSDecimalNumber:initWithString("2.3")
print(x)
print(x:class())

The output is

(0x631e054 => 0x631d1a0) 2.3
(0x631e924 => 0x25f618) NSCFNumber

instead of something like

(0x621e834 => 0x620c550) <NSDecimalNumber: 0x620c550>

Turning on full logging in wax give the following trace in the debug window:

Creating class for WaxServer(0x621bf40)
Storing reference of class to userdata table WaxServer(0x621bf40 -> 0x621c454)
Storing reference to strong userdata table WaxServer(0x621bf40 -> 0x621c454)
Creating class for NSDecimalNumber(0x261120)
Storing reference of class to userdata table NSDecimalNumber(0x261120 -> 0x6205e44)
Storing reference to strong userdata table NSDecimalNumber(0x261120 -> 0x6205e44)
Creating instance for NSDecimalNumberPlaceholder(0x6213450)
Retaining instance for NSDecimalNumberPlaceholder(0x6213450 -> 0x621d7c4)
Storing reference of instance to userdata table NSDecimalNumberPlaceholder(0x6213450 -> 0x621d7c4)
Storing reference to strong userdata table NSDecimalNumberPlaceholder(0x6213450 -> 0x621d7c4)
Creating instance for NSCFNumber(0x620c550)
Retaining instance for NSCFNumber(0x620c550 -> 0x621e834)
Storing reference of instance to userdata table NSCFNumber(0x620c550 -> 0x621e834)
Storing reference to strong userdata table NSCFNumber(0x620c550 -> 0x621e834)
(0x621e834 => 0x620c550) 2.3
Creating class for AppDelegate(0x621ec50)
:
:

Two things are showing in this log which I did not ask for, NSDecimalNumberPlaceholder and NSCFNumber. I believe these are the source of my grief and I have no idea where they are coming from. Any ideas on how to fix the issue?

Ultimately I want to call the method decimalValue, but wax complains that it can't call a method on a number.

Truelove answered 26/6, 2011 at 3:0 Comment(2)
would it make a difference if it were a dishonest NSDecimalNumber? ;)Ima
@Dave DeLong: as long as it can be converted to an NSDecimal.Truelove
L
2

NSDecimalNumber overrides -description to return the number it represents. When you're logging statement is printing "2.3", it is in fact printing the NSDecimalNumber object. You can verify this for yourself by calling -class on your x value and printing that as well.

Lovash answered 26/6, 2011 at 3:4 Comment(8)
What I'd like to do with the object is to call the decimalValue method. It complains that the number 2.3 does not have that method, so I don't believe it's faking what it's displaying.Truelove
Sounds like you're getting an NSNumber instead of an NSDecimalNumber back. I wonder if Wax is doing something weird here?Lovash
An NSNumber should also accepts decimalValue, so this is not an NSNumber. I tried the class method and it says it's and NSCFNumber.Truelove
@John Smith: Ah hrm. -decimalValue returns a struct. Does Wax even support structs?Lovash
@Kevin Ballard: yes if you include them in structs.luaTruelove
@John Smith: OK, because NSCFNumber is the "real" class that most NSNumbers are. It has to do with toll-free bridging.Lovash
NSNumber and CFNumber are toll free bridged. I have no seen any info on NSCFNumber from Apple.Truelove
NSCFNumber is an implementation detail.Lovash

© 2022 - 2024 — McMap. All rights reserved.