How to send 'number' and 'table' iOS to Corona?

I saw the document below.

https://docs.coronalabs.com/daily/coronacards/ios/communication.html

It shows only sending strings. but I want to send another file type. especially “table”.

My app is crashed when I send strings from objective-c to lua.

*** Terminating app due to uncaught exception of class '__NSCFConstantString’

libc++abi.dylib: terminating with uncaught exception of type __NSCFConstantString

 

How to send ‘number’ and ‘table’ iOS to Corona?

From Corona,  you build up a Lua table named event and dispatch it:

local event = { name="coronaView", message="Hello from CoronaCards!" } --dispatch the event to the global Runtime object local result = Runtime:dispatchEvent( event )

The event.name value most be “coronaView”. But after that, you can put in any valid Lua data type in the table. It could be:

local event = {} event.name = "coronaView" event.playerId = 10 event.isTrue = true

any way you want to create a key-value pair table.

On the native side, you’re receiving a NSdictionary which is the ObjC version of a key-value pair table.

- (id)coronaView:(CoronaView \*)view receiveEvent:(NSDictionary \*)event { NSLog( @"Logging properties of the event: %@", event ); self.myLabel.text = [event objectForKey:@"message"]; int playerId = [event objectForKey:@"playerId"]; BOOL isTrue = [event objectForKey:@"isTrue"]; return @"Nice to meet you CoronaCards!"; }

Or something similar to that.

Yeah. I know it. But my question is reverse. Objective-C to Lua.

I know below…

NSDictionary *event = [NSDictionary dictionaryWithObjectsAndKeys:
@“pauseEvent”, @“name”,
@“pause”, @“phase”,
@“otherValue”, @“otherKey”,
nil];
[coronaview sendEvent:event];

right. but it can use only string.

I wanna use number and table. Because my sending string is big. from native socket client.

Can I use number and table? If it is true, how? Thank you. (-:

You make an NSdictionary. It can contain things other than strings. Our example only uses strings.

NSDictionary \*otherTable = [NSDictionary dictionaryWithObjectsAndKeys: @"value1", @"key1", @"value2", @"key2", nil]; CoronaView \*view = ... NSDictionary \*event = [NSDictionary dictionaryWithObjectsAndKeys: @"pauseEvent", @"name", @"pause", @"phase", [NSNumber numberWithInt:1], @"count", @"otherValue", @"otherKey", otherTable, @"otherTable", nil]; [view sendEvent:event];

You have to have “Objects”, not simple variables. See: http://stackoverflow.com/questions/8991535/can-nsdictionary-contain-different-types-of-objects-as-values

Your other table would have to be an NSDictionary object. I’ve not tested the code, but based on what I’ve seen, this should work. Oddly though most tutorials only show strings. You can always tonumber() the values to convert them back to strings if you did send strings.

Rob

Oh. I didn’t feed back.

OK. It works good. Thank you. (-:

From Corona,  you build up a Lua table named event and dispatch it:

local event = { name="coronaView", message="Hello from CoronaCards!" } --dispatch the event to the global Runtime object local result = Runtime:dispatchEvent( event )

The event.name value most be “coronaView”. But after that, you can put in any valid Lua data type in the table. It could be:

local event = {} event.name = "coronaView" event.playerId = 10 event.isTrue = true

any way you want to create a key-value pair table.

On the native side, you’re receiving a NSdictionary which is the ObjC version of a key-value pair table.

- (id)coronaView:(CoronaView \*)view receiveEvent:(NSDictionary \*)event { NSLog( @"Logging properties of the event: %@", event ); self.myLabel.text = [event objectForKey:@"message"]; int playerId = [event objectForKey:@"playerId"]; BOOL isTrue = [event objectForKey:@"isTrue"]; return @"Nice to meet you CoronaCards!"; }

Or something similar to that.

Yeah. I know it. But my question is reverse. Objective-C to Lua.

I know below…

NSDictionary *event = [NSDictionary dictionaryWithObjectsAndKeys:
@“pauseEvent”, @“name”,
@“pause”, @“phase”,
@“otherValue”, @“otherKey”,
nil];
[coronaview sendEvent:event];

right. but it can use only string.

I wanna use number and table. Because my sending string is big. from native socket client.

Can I use number and table? If it is true, how? Thank you. (-:

You make an NSdictionary. It can contain things other than strings. Our example only uses strings.

NSDictionary \*otherTable = [NSDictionary dictionaryWithObjectsAndKeys: @"value1", @"key1", @"value2", @"key2", nil]; CoronaView \*view = ... NSDictionary \*event = [NSDictionary dictionaryWithObjectsAndKeys: @"pauseEvent", @"name", @"pause", @"phase", [NSNumber numberWithInt:1], @"count", @"otherValue", @"otherKey", otherTable, @"otherTable", nil]; [view sendEvent:event];

You have to have “Objects”, not simple variables. See: http://stackoverflow.com/questions/8991535/can-nsdictionary-contain-different-types-of-objects-as-values

Your other table would have to be an NSDictionary object. I’ve not tested the code, but based on what I’ve seen, this should work. Oddly though most tutorials only show strings. You can always tonumber() the values to convert them back to strings if you did send strings.

Rob

Oh. I didn’t feed back.

OK. It works good. Thank you. (-: