Simple Wax framework tutorial (3) getting started with Wax

Source: Internet
Author: User


This is a quick tutorial on the Wax framework. Through this tutorial, you will eventually understand how interesting it is to use Wax in OC.

Wax is the adhesive between OC and Lua. That is to say, you can communicate between OC and Lua.
Using Wax to access the Cocoa class with Lua is as simple as in OC.

NSString -- NSString returned

UIView -- Return UIView


UITableView -- the "syntactic sugar" in the following line"

Wax. class ["UITableView"] -- native Wax call, you do not have to understand



When you create an OC instance, you do not need to use alloc. Wax will do this for you.
-- Oh, no need for alloc, retain, and release!

Local view = UIView. initWithFrame (CGRect (0, 0,100,100 ))

Create an OC class with Lua and use the waxClass {CLASS_NAME, PARENT_CLASS} function }. All subsequent functions (in the same Lua file) will be automatically added as the instance method of this class.
WaxClass {"MyClass", NSObject}


-- Declaration Protocol

WaxClass {"MyClass", NSObject, protocols = {"UITableViewDelegate", "UITableViewDataSource "}}


-- Note! Protocol names are in the string format (because they are not classes) REMEMBER!

When calling the Lua function, use {} Instead of () (see waxClass function call ). This is the "syntactic Sugar" that uses a Lua table to pass parameters ".
-- Equivalent to 2nd sentences

WaxClass {"MyClass", NSObject}

-- Braces can be ignored, equivalent to 1st sentences.

WaxClass ({"MyClass", NSObject}) --... omitting the parenthesis ismuch more pretty, so that's how we roll.

For waxClass functions, the 1st parameters are always self, so that Wax can be used to simulate the object-oriented model except OC.

WaxClass {"MyClass", NSObject}


-- Note that the 1st parameters of the function are self. This holds the MyClass instance object.

FunctionstoreWords (self, words)

Self. words = words

-- Wax creates a 'super' member variable in the self object.

-- To Call The parent class Method

Self. super: words (self)




Use the colon (:) instead of the dot (.) to call the OC method. This will pass the caller as 1st parameters to this method.
-- In Lua, use the colon (:) For method call.

UIApplication: sharedApplication ()

--... Is equivalent

UIApplication. sharedApplication (UIApplication)

-- Use the colon (:) to make the code simple and readable

If the OC method has multiple parameters, use underscore _ instead of Colon :.

[UIAlertView initWithTitle: @ "title" message: @ "message" delegate: nil];

Use Wax

UIAlertView: initWithTitle_message_delegate ("title", "message", nil)

Wax does not use the OC attribute. It forces Lua and OC to communicate only through method calls.

SomeView. frame -- this is not the case


-- Only the getter/setter method can be used.

SomeView: frame ()

SomeView: setFrame (someFrame)


You can dynamically create member variables for an object by using the vertex number. Unlike the colon: (Class/instance method used to call OC), you can dynamically create member variables in the Lua code (these variables are unknown in the object's OC code ). These member variables are valid in the object lifecycle.
-- Use a colon in Lua to call a method...

Localview = UIView: init ()

View. someRandomVariable = "YEAH! "

-- You can assign any object to an instance and it will always exist.

The Wax view converts the OC object to the original Lua type. If the OC method has an NSString parameter, you can directly pass a Lua string to it.
Local fileManager = NSFileManager: defaultManager ()

Local contents = fileManager: directoryContentsAtPath (".")

-- DirectoryContentsAtPath returns an NSArray, but Wax converts it to Lua table

Print (contents [1]) --> "info. plist"

-- NSDictionaries becomes Lua tables

-- NSArrays is changed to Lua tables

-- NSStrings is changed to Lua strings

-- NSNumbers is changed to Lua numbers.

If the OC object is not automatically converted, you can use the toobjc method.

-- If you call NSString in this way, it will not work.

Local string = "astring"

String: capitalizedString ()

-- This is because the string has been forcibly converted to Luastring.


-- Use toobjc to prevent NSString from being forcibly converted to Luastring

Toobjc (string): capitalizedString ()



Enumeration type, such as UITableViewStylePlain, has been hard-coded. Currently, most common enumeration types are defined in APP_ROOT/wax/stdlib/enums. lua. In the future, I want to use bridging in the iPhone so that enumeration and structure can be dynamically created.
To pass a selector, use a string directly. For example, in OC, selector is written as @ selector (this: is: a: method) and this: is: a: method in Wax.
Struct is also the same as enumeration. The most common schema bodies are declared in APP_ROOT/wax-scripts/structs. lua. According to the template in the structs. lua file, it is easy to add a new struct.

Wax. struct. create ("CGRect", "ffff", "x", "y", "width", "height ")


-- Create a global function named CGRect with four float Parameters

-- The 2nd parameter "ffff" defines the types of the four parameters


Local rect = CGRect (1, 2, 3, 4)

Print (rect. x) --> 1

Rect. x = 200

Print (rect. x) --> 200


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: 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.