Brief Wax framework tutorial (1) Overview

Source: Internet
Author: User

1. What is Wax?

Wax is a framework for compiling Local iphoneapps in lua. It uses the OC runtime to bind the OC and Lua. With Wax, you can use Lua to do anything that OC can do! What are you waiting? Get started!

 

2. Why write an iPhone app with Lua?

I like to write iPhone apps, but I only want to write them in dynamic languages rather than OC. Why do many people prefer Lua + Wax instead of OC? There are several reasons:

Q: automatic garbage collection! The days of alloc, retain, and release are gone forever!

Q: less code! No header files, no static types, arrays, or dictionary statements! Lua reduces the number of lines in your code.

Q can access Cocoa, UITouch, and Foundation frameworks, each of which is quite a lot! Wax automatically exposes the OC framework to Lua, and each framework you want is still valid!

QHTTP requests are simpler. It's never that easy to interact with RESTwebservice!

QLua supports closures, that is, block statements! People who have used them know how powerful they are.

QLua has a built-in Regular Expression matching library.

Iii. Example

For a simple example of Wax app, see the examples folder.

 

How do I create a UIView and set it to red?

 

-- Forget alloc! The memory is automatically managed by Wax.

View = UIView: initWithFrame (CGRect (0, 0,320,100 ))

-- Send a message to the OC object using a colon

-- All methods of the UIView object can be accessed using this method.

View: setBackgroundColor (UIColor: redColor ())

 

How to call multiple parameters?

 

-- Add an underline connection parameter to the method name and use it like a normal C function.

UIApplication: sharedApplication (): setStatusBarHidden_animated (true, false)

 

How do I pass the array/string/dictionary parameter?

 

-- Wax automatically converts array/string/dictionary to NSArrayv/NSString/vNSDictionary (and inverse conversion)

Images = {"myFace.png", "yourFace.png", "theirFace.png "}

ImageView = UIImageView: initWithFrame (CGRect (0, 0,320,460) imageView: setAnimationImages (images)

 

How to Create a subclass of UIViewController?

 

-- Create in "MyController. lua"

-- Create an OC class MyController that inherits from UIViewController

-- This is actually an OC object. If necessary, you can even reference it in the OC code.

WaxClass {"MyController", UIViewController}

Functioninit ()

-- To Call The parent class method, explicitly use self. super

Self. super: initWithNibName_bundle ("MyControllerView. xib", nil)

Returnself

End

FunctionviewDidLoad ()

-- Write Other code here

End

 

You said the HTTP call would be easy. I don't believe it...

 

Url = "http://search.twitter.com/trends/current.json"

-- Asynchronous call. When a response is received, the callback function is called.

Wax. http. request {url, callback = function (body, response)

-- Send NSHTTPURLResponse request

Puts (response: statusCode ())

-- Because content-type is json, Wax automatically parses and encapsulates the content into the Luatable type.

Puts (body)

End}

 

Because Wax automatically converts NSString, NSArray, NSDictionary, and NSNumber to native Lua values, sometimes you must forcibly convert them back to the OC object, for example:

 

Local testString = "Hello lua! "

Local bigFont = UIFont: boldSystemFontOfSize (30)

Local size = toobjc (testString): sizeWithFont (bigFont)

Puts (size)

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.