Turn--object-c with LUA using wax interaction

Source: Internet
Author: User

"51CTO exclusive translation" June 2011 's programming language leaderboards the Lua language one of the top 10. This was a year ago when Apple decided to use the Lua language on iOS systems. But do you know how to develop apps in the Lua language on iOS? 51CTO will introduce you to the LUA language iOS application Development Framework--wax, where the extremely hot "Angry Birds" on the iOS platform was developed by the Lua language with Wax. The full text is divided into two parts, the first part of 51CTO will take you to explore some of the benefits of wax, while demonstrating the practical steps necessary to integrate LUA with Xcode 4 and the iOS Software Development Kit (SDK). The second part of 51CTO will step through how to build a simple application with wax, display a list of current trending topics on Twitter, and use buttons to update the content.

What is wax?

Wax for iphone This framework was developed to combine the LUA scripting language with the native OBJECTIVE-C application programming Interface (API). This means that you can use any and all of the objective-c classes and frameworks from within Lua.

Technically, wax combines the Objective-c class with the native C code. The LUA language embeds the C language, and then the Objective-c class is incorporated into it.

Why use wax?

Wax is free and open-source . Unlike some other LUA-based mobile development solutions, wax is an open source framework that takes you a little bit of time to get started without spending money. Do you not like the way wax works, or do you find defects in implementation? The source code is available for free, and you can always change the source code to suit your needs.

native APIs can be leveraged. This means that tutorials written for teaching objective-c can easily be modified and written by Lua for wax. This also means that your application looks like a native application in its appearance, but it also has the benefit of saving time by writing code in the efficient scripting language of LUA.

You can use Xcode. This means that simulators and device deployments are a breeze, and will not easily break with future versions of iOS.

you can take advantage of all existing objective-c libraries . If you have a objective-c class that you've written before and you don't need to change it, you can use it in Lua-just put it in Xcode. The same is true for libraries like THREE20. You can access them using LUA code as long as the libraries are added as normal instructions.

The wax LUA module can be leveraged . Wax has several built-in LUA modules that make asynchronous HTTP requests and JavaScript Object Callout (JSON) creation/parsing extremely easy and fast (because the modules are written in C).

There is no need to manage memory . You no longer need to worry about transactions such as memory allocations. Wax to handle all this for you.

the Lua type is automatically converted to the corresponding objective-c type, and vice versa . This means that if you call a method that requires nsstring and Nsinteger, but you pass the LUA string and the Lua integer, wax will take care of the conversion work for you. This conversion is powerful and can even handle complex objective-c features, such as selectors.

You can take advantage of all the above features . No need to choose carefully. You get all the features!

OK, that's great! How do I install wax?

First you need Xcode and the iphone SDK. If you don't have any of these things, get a copy!

Create a project with Xcode

Let's start by creating a new "Windows-based project" called "Waxapplication". Don't forget to set up your device as an iphone:

Browse to the folder where you saved the item through the Finder. Create three new folders: wax, scripts, and classes. Your folder should look like this:

Set wax (Part one, working with files)

First, download the source code for the compressed package. Wax on GitHub (https://github.com/probablycorey/wax), it's easy to download the source code. Download the compressed package from here.

Now, unzip the file you just downloaded. Browse to the folder you just unzipped. It will have names such as "Probablycorey-wax-124ca46".

Your screen should look like this now:

Now, take the following actions:

Copy the Lib and Bin folders and paste them into the wax folder in the Waxapplication project folder.

Copy the Xcode-template/classes/protocolloader.h to the Waxapplication project folder.

Copy the xcode-template/scripts/folder and put it in the Waxapplication project folder.

Open the wax/lib/extensions/folder inside the Waxapplication project folder. Delete the SQLite and XML folders, as shown in:

Your screen should look like this:

Set wax (Part two, configuration items)

Now use the Finder to select the classes, scripts, and wax folders and drag them into the Xcode project. Put them below the one that shows "Wax application" and "1 target, IOS SDK x.x". Do not tick the checkbox showing "Copy items into Destination Group's folder (if needed)". Click Done.

Now click on the one that says "Wax application" and "1 target, IOS SDK 4.3". Then take the following steps:

In the right pane, look for the "Targets" heading and click "Waxapplication". Click the Build Phases tab. Click "Copy Bundle Resources" (copy bundle Resource) to clear all LUA files.

In the lower-right corner, click "Add Build Phase" (add build phase), and then click "Add Run Script".

Set the Shell to/bin/zsh

Set the text area below the shell to "$PROJECT _dir/wax/lib/build-scripts/copy-scripts.sh".

Your screen now looks like this:

Change MAIN.M

In the left pane, open the folder named "Waxapplication." Next, open the supporting Files folder. Next, open the MAIN.M and change the contents of the file to read as follows:

  1. //This is the place where miracles happen!
  2. Wax does not use the nib file to load the main view, everything is done in the Appdelegate.lua file
  3. #import <UIKit/UIKit.h>
  4. #import "Wax.h"
  5. #import "Wax_http.h"
  6. #import "Wax_json.h"
  7. #import "Wax_filesystem.h"
  8. int Main (int argc, char *argv[]) {
  9. NSAutoreleasePool * Pool = [[NSAutoreleasePool alloc] init];
  10. Wax_start ("Appdelegate.lua", Luaopen_wax_http, Luaopen_wax_json, Luaopen_wax_filesystem, nil);
  11. int retVal = Uiapplicationmain (argc, argv, nil, @"appdelegate");
  12. [Pool release];
  13. return RetVal;
  14. }

Don't forget to save the file!

Delete unnecessary files

Delete Mainwindow.xib, WaxApplicationAppDelegate.h, and waxapplicationappdelegate.m three files. Open "Waxapplication/supporting files/waxpallication-info.plist", and then delete the line that is "Main nib file base name".

Test the installation of wax

You can run the application in the emulator by pressing the ↵ (command + Enter), or by pressing "run" in the upper-left corner. If everything works, you'll see a simple app that says "Hello lua!".

If you don't see the message, check the previous steps to see if there are any steps missing.

View Lua

Expand the Scripts folder and open Appdelegate.lua. You will see the LUA code that runs the application.

You may notice first that there is no syntax highlighting. Unfortunately, for the LUA syntax highlighting problem in Xcode, I have not found a stable solution (you found out, please leave a message to tell me!).

Next you may notice that there are no square brackets, but the classes such as UIScreen and UIWindow are used. That's because you're using LUA to build a appdelegate class; in Apple and Apple's code, you're using OBJECTIVE-C, building OBJECTIVE-C classes!

Method name

You may also notice the strange method name "Colorwithred_green_blue_alpha". If you're familiar with objective-c, you know the method name can have a colon. Function names in Lua cannot have colons. In order to compensate for this discrepancy, any place in the objective-c where the name of the method is separated is underlined in Lua. For example:

The ColorWithRed:green:blue:alpha in objective-c corresponds to the Colorwithred_green_blue_alpha in Lua.

SelectRowAtIndexPath:animated:scrollPosition in Objective-c: corresponds to Selectrowatindexpath_animated_scrollposition in Lua

Object-oriented model

Another problem with LUA is that it has no inheritance system like objective-c. There is no class at all in Lua. To overcome this problem, wax highlights a function placed at the front of each wax Lua file: Waxclass. In the default Appdelegate.lua, this line looks like this:

    1. waxclass{"Appdelegate", protocols = {"Uiapplicationdelegate"}}

To create a objective-c class from Lua, use the function waxclass{"class NAME", "Parent_class"}. All subsequent functions that you add to the Lua file (inside the same file) are automatically added to the new class as an instance method.

This line of Appdelegate.lua shows that you can also define which protocols your class defines.

Although Waxclass solves the problem of defining classes that objective-c can use, there is still a problem: Because LUA does not have classes, it does not have dynamic "self" variables like objective-c. To overcome this problem, wax automatically takes the first variable of each method as the current instance of the class. You can see that when you look at "applicationdidfinishlaunching" in Appdelegate.lua, the first variable is the "from" variable, even though the objective-c version of this method has only 1 variables. However, if you have to transfer the current instance of the class as the first variable of each method, it is annoying and adds some grammatical conveniences (syntactical sugar). Not using "." The operator makes a "method" call in Lua, instead uses the ":" Operator:

    1. Local view = Uiview.initwithframe (cgrect (0, 0, 100, 100))

--Identical to the following

    1. View:addsubview (Someview)
    2. Iew.addsubview (view, Someview)

Another important aspect worth mentioning is that wax does not support the Objective-c property. Wax forces Lua and objective-c to contact only the method.

--that's not.

    1. Someview.frame

--You need to use the Getter/setter method

    1. View:frame () some
    2. View:setframe (Someframe)

Variables for LUA only

You can use the dot. Operator to create a member variable for any Objective-c object. Unlike the colon: operator (used to invoke a method on a Objective-c class/instance), the point. Operator can dynamically create member variables (the objective-c aspect of the object does not know about these variables) for the LUA aspect of the object. Member variables can be used within the life cycle of an object.

Output to console

Appdelegate.lua also shows how you can write debug text, output to the console. You can use the function "puts".

Memory management

As I said before, with Lua, you don't have to allocate, retain, and free memory at all. You do not need to call memory allocations at all before invoking any initializers. In fact, if you do this, the program may have a memory leak.

What's the next thing?

Now that you have a thorough understanding of the basics of LUA specifically for wax, you're ready to write iphone apps!

In the second part of this tutorial, we'll be able to write a Twitter sample application with a refresh button in just a few lines of Lua.

Did you have the confidence to create your own Angry Birds after reading this tutorial?

Tips

The LUA programming language is a concise, lightweight, extensible scripting language. Lua Reads/' lua/(chatter), is the Portuguese language "Luna" (moon) meaning.

Lua is a lightweight language, and its official version includes only a streamlined core and the most basic library. This makes Lua smaller and faster to boot. It is written in standard C language and open in source code form, only 100 k after compilation, it can be easily embedded in other programs. Unlike many "chatty" languages, Internet communications, graphical interfaces, etc. are not provided by default. But Lua can be easily extended: These features are provided by the host language (usually C or C + +), and Lua can use them as if they were built-in functionality. In fact, there are a lot of mature expansion modules to choose from now.

The goal of Lua is to become a language that is easy to embed in other languages. Most programmers also think that it does.

Many applications use LUA as their embedded scripting language for configuration and extensibility. This includes the West Tour II, Wonderland Legends, World of Warcraft, the hammer 40k, the door of the Bo Tak, the Sword of the Chinese cloud, the most famous in the mobile field is the "Angry Birds."

Go--object-c and Lua use wax interaction

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.