Wax Framework Concise Tutorial (3) Start using wax

Source: Internet
Author: User
Tags instance method lua

Overview

This is a quick tutorial for the wax framework. Through this tutorial, you will eventually understand how interesting it is to use wax in OC. Wax is the binder between OC and Lua. In other words, you can communicate between OC and LUA. with wax, using LUA to access the Cocoa class is as simple as in OC.

NSString--Back to NSString

UIView--Back to UIView

UITableView--"grammatical sugars" in the following line

wax.class["UITableView"]--wax native invocation, you don't have to understand

When you create an OC instance, you do not need to use Alloc,wax to do so for you.

-Oh, my God, no need for alloc,retain and release!

The Local view = Uiview.initwithframe (CGRect (0, 0,100, 100)) creates the OC class with LUA, using the function Waxclass{class_name,parent_class}. All subsequent added functions (in the same LUA file) are automatically added as instance methods for the class.

waxclass{"MyClass", NSObject}

--Declaration agreement

waxclass{"MyClass", nsobject, protocols = {"Uitableviewdelegate", "Uitableviewdatasource"}}

--note. The protocol name is a string form (because they are not classes) remember! When you call the LUA function, use {} instead of () (see the call to the Waxclass function). This is a "syntactic sugar" that passes arguments with a LUA table.

--equivalent to the 2nd sentence

waxclass{"MyClass", NSObject}

--curly braces can be ignored, equivalent to the 1th sentence

Waxclass ({"MyClass", nsobject})--... omitting the parenthesis ismuch more pretty, so that's how we roll. For the Waxclass function, the 1th argument is always self, in order to simulate the object-oriented model of OC with wax.

waxclass{"MyClass", NSObject}

-Note that the 1th parameter of the function is self. This holds the MyClass instance object

Functionstorewords (self, words)

Self.words = words

--Wax creates a member variable of ' super ' in the Self object

--to invoke the parent class method

Self.super:words (self)

End

Use a colon: instead of a point number. To invoke the OC method. This passes the caller itself to the method as the 1th argument.

--in Lua, using colons: Making method calls

Uiapplication:sharedapplication ()

--... Equal to

Uiapplication.sharedapplication (uiapplication)

--use colon: To make the code simple and readable if the OC method has more than one argument, underline _ instead of a colon:.

In the OC

[Uialertview initwithtitle:@ "title" message:@ "message" delegate:nil];

Using Wax

Uialertview:initwithtitle_message_delegate ("title", "message", nil) wax do not use the OC attribute. It forces LUA and OC to communicate only through method modulation

Someview.frame--that's not right.

--Only by Getter/setter method

Someview:frame ()

Someview:setframe (Someframe)

Through the dot number. You can create member variables dynamically for an object.  differs from the colon: (The class/instance method used to invoke OC), in the Lua code midpoint. The ability to dynamically create member variables (in the object's OC code are completely unaware of these variables). These member variables are valid in the object lifecycle.

--Using colons in Lua: Making method calls ...

Localview = Uiview:init ()

View.somerandomvariable= "yeah!"

You can assign any object to an instance, and it will always exist wax view converts the OC object to the LUA original type. Conversely, if the OC method has a nsstring argument, you pass a LUA string directly to it.

Local FileManager =nsfilemanager:defaultmanager ()

Local contents =filemanager:directorycontentsatpath (".")

--Directorycontentsatpath returns a nsarray, but Wax converts it to a Lua table

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

--Nsdictionaries into Lua tables

--Nsarrays into LUA tables

--nsstrings into LUA strings

-Nsnumbers becomes LUA numbers if you are not automatically converted as an OC object, you can use the TOOBJC method.

--If you call NSString this way, it's not going to work.

Local string = "astring"

String:capitalizedstring ()

--This is because string has been cast to luastring.

--use TOOBJC to prevent NSString from being cast to luastring

TOOBJC (String): Capitalizedstring ()

Enumeration types, such as Uitableviewstyleplain, have been hard-coded. Currently, most commonly used enumerations are defined in App_root/wax/stdlib/enums.lua. I would like to use bridging in the iphone in the future so that enumerations and structs can be dynamically created. To pass a selector, use the string directly. For example, in OC, selector is written as @selector (This:is:a:method), written this:is:a:method in wax. The struct body is also the same as the enumeration. The most commonly used structural bodies are declared in App_root/wax/wax-scripts/structs.lua. It is easy to add new structures based on the templates in the Structs.lua file.

Wax.struct.create ("CGRect", "FFFF", "X", "Y", "width", "height")

--Create a global function called CGRect, with 4 float parameters

--The 2nd parameter "FFFF" defines the type of 4 parameters

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

Print (rect.x)--> 1

Rect.x = 200

Print (rect.x)--> 200


http://blog.csdn.net/kmyhy/article/details/8048091

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.