Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Communication from LUA to iOS (ObjC or Swift) - Array Format
Started by remyb Feb 18 2018 03:22 PM

2 replies to this topic
communication coronacards array format encoding parsing
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

remyb

[GLOBAL: userInfoPane.html]
remyb
  • Observer

  • 9 posts
  • Corona SDK

Hello,
 
I have a question regarding the method communication from Lua to Swift.
I know that Lua has a special way to handle arrays & associative arrays as the same kind of objects.
 
Here is the situation:
 
1. Let's say I fill an array with the players ids.
local playersIds = {}
table.insert(playersIds, 'Player A')
table.insert(playersIds, 'Player B')
2. Then I send this array to the app.
local event = { name='coronaView', event='PlayersIdsUpdated', playersIds=playersIds }
Runtime:dispatchEvent(event)
3. I would expect to receive a NSArray/Array in the app. Instead, I'm receiving a Dictionary<Int,String>.
 
The thing is that I want to convert this list to JSON, and send it somewhere.
But the JSONSerialization class on iOS doesn't handle Dictionary<Int,String> values. The conversion just fails.
 
----
 
I know, there are a lot of solutions here, and it's not a real problem.
 
As I'm just starting Lua & Corona, I was just wondering if there is a way to get directly a NSArray from Corona, without implementing something specific in Swift.
 
Thank you!
 
PS: The JSON package knows how to differentiate dictionaries and arrays at the encoding (https://docs.coronalabs.com/api/library/json/encode.html).


[TOPIC: post.html]
#2

remyb

[GLOBAL: userInfoPane.html]
remyb
  • Observer

  • 9 posts
  • Corona SDK

Well, I ended up doing a little work in Swift...

func cleanupLua(_ object: Any) -> Any {
    
    if let dico = object as? [Int:Any] {
        return dico.values.map { cleanupLua($0) }
        
    } else if let dico = object as? [String:Any] {
        return dico.mapValues { cleanupLua($0) }
        
    } else if let array = object as? [Any] {
        return array.map { cleanupLua($0) }
        
    } else {
        return object
    }
}


[TOPIC: post.html]
#3

StarCrunch

[GLOBAL: userInfoPane.html]
StarCrunch
  • Contributor

  • 824 posts
  • Corona SDK

Hi.

 

table.insert(playersIds, 'something') or playersIds[#playersIds + 1] = 'something' will both populate your array starting at index 1, rather than 0. Might this be throwing off some sort of array-vs.-dictionary detection?




[topic_controls]
[/topic_controls]