List to array conversion

Working around Linq…

In Maze Rat, we would like to save the game field data (maps) of custom levels to a Parse database. In Unity, we have stored the data in a two-dimensional list of strings before, but Parse requires an (multidimensional) array.

With Linq this would be as easy (source: Stack Overflow):

string[][] myArray2D = myList2D.Select(a => a.ToArray()).ToArray();

So what’s the big deal?

Unfortunately, we learned that Linq is to be avoided as it is not compatible with iOS. Actually, we are not doing any iOS builds so far, but we are trying to keep our code compatible with iOS for the future.

This was really a bit of pain when we first noticed that and had to rewrite a class that contained some simple implementations from the Linq-functions. We called it ReLinq. Today, we have extended our ReLinq with the conversion of multidimensional lists to multidimensional arrays (strictly speaking, this is not directly a function of Linq, but we have to put it somewhere^^).

You call the function like this:

string[][] myArray2D = myList2D.ListToArrayConversion2D();

Explanation what’s happening:

  • Create the array which is returned and set the size of the first dimension (line 3)
  • Iterate through the first dimension (line 4)
  • Save the list into a variable for easier access (line 6)
  • Define the array size of the second dimension (line 7)
  • Iterate through the second dimension and copy the elements from the list into the array (line 8)
  • Return the result array (line 10)

Naturally, you could easily extend the two dimensions with more dimensions if you like, but for us, two dimensions are sufficient.

Maybe you’ve come across similar issues? Have you found other challenges with iOS compatibility?

Keep hacking,

Your Fantasy Arts Team

Share on FacebookShare on Google+Tweet about this on TwitterShare on TumblrShare on LinkedInEmail this to someone

Be the first to comment

Leave a comment

Your email address will not be published.