博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
wax
阅读量:6541 次
发布时间:2019-06-24

本文共 3316 字,大约阅读时间需要 11 分钟。

ax is a framework that lets you write native iPhone apps in . It bridges Objective-C and Lua using the Objective-C runtime. With Wax, anything you can do in Objective-C is automatically available in Lua! What are you waiting for, give it a shot!

Why write iPhone apps in Lua?

I love writing iPhone apps, but would rather write them in a dynamic language than in Objective-C. Here are some reasons why many people prefer Lua + Wax over Objective-C...

  • Automatic Garbage Collection! Gone are the days of alloc, retain, and release.

  • Less Code! No more header files, no more static types, array and dictionary literals! Lua enables you to get more power out of less lines of code.

  • Access to every Cocoa, UITouch, Foundation, etc.. framework, if it's written in Objective-C, Wax exposes it to Lua automatically. All the frameworks you love are all available to you!

  • Super easy HTTP requests. Interacting with a REST webservice has never been eaiser

  • Lua has closures, also known as blocks! Anyone who has used these before knows how powerful they can be.

  • Lua has a build in Regex-like pattern matching library.

Examples

For some simple Wax apps, check out the .

How would I create a UIView and color it red?

-- forget about using alloc! Memory is automatically managed by Waxview = UIView:initWithFrame(CGRect(0, 0, 320, 100))-- use a colon when sending a message to an Objective-C Object-- all methods available to a UIView object can be accessed this wayview:setBackgroundColor(UIColor:redColor())

What about methods with multiple arguments?

-- Just add underscores to the method name, then write the arguments like-- you would in a regular C functionUIApplication:sharedApplication():setStatusBarHidden_animated(true, false)

How do I send an array/string/dictionary

-- Wax automatically converts array/string/dictionary objects to NSArray,-- NSString and NSDictionary objects (and vice-versa)images = {"myFace.png", "yourFace.png", "theirFace.png"}imageView = UIImageView:initWithFrame(CGRect(0, 0, 320, 460))imageView:setAnimationImages(images)

What if I want to create a custom UIViewController?

-- Created in "MyController.lua"---- Creates an Objective-C class called MyController with UIViewController-- as the parent. This is a real Objective-C object, you could even-- reference it from Objective-C code if you wanted to.waxClass{"MyController", UIViewController}function init()  -- to call a method on super, simply use self.super  self.super:initWithNibName_bundle("MyControllerView.xib", nil)  return selfendfunction viewDidLoad()  -- Do all your other stuff hereend

You said HTTP calls were easy, I don't believe you...

url = "http://search.twitter.com/trends/current.json"-- Makes an asyncronous call, the callback function is called when a-- response is receivedwax.http.request{url, callback = function(body, response)  -- request is just a NSHTTPURLResponse  puts(response:statusCode())  -- Since the content-type is json, Wax automatically parses it and places  -- it into a Lua table  puts(body)end}

Since Wax converts NSString, NSArray, NSDictionary and NSNumber to native Lua values, you have to force objects back to Objective-C sometimes. Here is an example.

local testString = "Hello lua!"local bigFont = UIFont:boldSystemFontOfSize(30)local size = toobjc(testString):sizeWithFont(bigFont)puts(size)

转载地址:http://jusdo.baihongyu.com/

你可能感兴趣的文章
Centos7安装搜狗输入法
查看>>
nodjs html 转 pdf
查看>>
Python字典
查看>>
ofstream 的中文目录问题
查看>>
Android存储方式之SQLite的使用
查看>>
springcloud ribbon 客户端负载均衡用法
查看>>
洛谷P1287 盒子与球 数学
查看>>
自定义starter
查看>>
Bootstrap vs Foundation如何选择靠谱前端框架
查看>>
JAVAWEB 一一 Hibernate(框架)
查看>>
与、或、异或、取反、左移和右移
查看>>
jQuery根据元素值删除数组元素的方法
查看>>
vue常用的指令
查看>>
matlab练习程序(随机游走图像)
查看>>
Linux命令行下运行java.class文件
查看>>
input文本框实现宽度自适应代码实例
查看>>
C#基本数据类型 <思维导图>
查看>>
POJ3321 Apple Tree (树状数组)
查看>>
protocol buffers的编码原理
查看>>
行为型设计模式之命令模式(Command)
查看>>