This article discusses three ways to create an iOS app with Lua. This includes creating a complete application (Corona) with Lua until using Lua as a script element in the app (through Wax or diy ). Before that, we need to ask ourselves two questions:
1. Why use Lua?
2. Does Apple allow Lua?
These two problems are closely related.
If you have no idea about Lua before, I will briefly introduce Lua. If you are familiar with Lua, skip this section.
About Lua
Lua is an efficient, lightweight, and embedded scripting language. Similar to Javascrip, Ruby, or Python. Many users like me think that Lua is a simple and elegant language.
Lua started in 1993 at the Catholic University of Rio de Janeiro in Rio de Janeiro, Brazil, including Robert Ierusalimschy, Waldemar Celes, and Luiz Henrique de Figueiredo. It applies to Mi Casa Verde, Adobe Lightroom, Celestia, lighttpd, LuaTeX, nmap, Wireshark, Cisco's Adaptive Security Appliance, pbLua, and thousands of games, including Grim Fandango, World of Warcraft, and Tap Revenge ). Lua uses the MIT protocol, which means Lua basically has no impact on commercial and non-commercial purposes.
Lua's main data construction mechanism is the combination of variable arrays and hash tables. List 1 lists a Lua Table, which is used to describe the vehicle's mileage per gallon. We can use string keys to store vehicle information, such as license and make. The use of digital subscript indexes to store a series of miles per gallon.
List 1: a Lua Table Type
Car_data = {license = 'xvw1942 ', make = 'volv', model = 'xc70', 30, 31.3, 32.4, 34.0}
Print (car_data [1]) -- 30
Print (car_data ['license ']) -- XVW1942
Print (car_data.license) -- XVW1942 (also !)
In Lua, the array subscript starts from 1, instead of 0. '--' Indicates that the comment starts until the end of the line. What you cannot think of is that Lua is neither an object-oriented language nor a functional programming language. However, it also provides several mechanisms that allow you to customize your advanced features. Lua contains various object systems, such as traditional OO systems and non-class OO systems (such as Oracle's Self language and Io language. Note * both of them are fan-based languages ). Lua supports the "first class function" (the Translator's note * is actually an anonymous function. Lua can construct a function at any time during runtime and regard it as an object ), closures and metadata features (such as metadata tables and metadata ). Lua can meet the needs of functional programming.
For introduction to Lua's object-oriented programming, please read Lua programming (This book provides a good introduction to all aspects of Lua ). You should also read the Lua example on Luawiki.
List 2 lists an implementation of the linked list class. The Variable List is actually a table, used as the meta table of all linked List objects. It implements a kind of event processing mechanism similar to the class for backward searching table indexes. "List. _ index = List" allows us to create a method for the List object. The method is to save the function in the List meta table. When we call these functions of the list object, we will find and run these functions in the List meta table.
This code shows a series of enhanced features of Lua: multi-value assignment (and function return multiple return values), method call syntax sugar (The role of the ':' symbol, similar to adding a self parameter to function calls, which can be seen in many languages, from Python to OC ).
List 2: Linked List class
List = {}
List. _ index = List
Function List: new ()
Local l = {head = {}, tail = {}, size = 0}
L. head. _ next, l. tail. _ prev = l. tail, l. head
Return setretriable (l, self)
End
Function List: first ()
If self. size> 0 then
Returnself. head. next
Else
Return nil
End
End
Function List: addFirst (elem)
Local node = {prev = self. head, value = elem,
Next = self. head. next}
Node. next. prev = node
Self. head. next = node
Self. size = self. size + 1
End
Mylist = List: new ()
Mylist: addFirst (12)
Print (mylist: first ())
Here, I ignore some important and interesting things (such as closures ). But at least you have learned a little Lua. When we enter the iPhone encoding later, we will see more Lua code.