Writing Objective-C apps in Lua

Now as Absurd as that may sound, what if you could write your objective-C apps in Lua? The instant advantages would be that there would be no allocation and deallocation of memory, however there can be memory leaks if you are not careful.

So what's the big miraculous thing? It is lua binding on an Objective-C app called WAX. For the sake of this article, no special code that has been written, this is the stock code that is created when you select a new Wax project.

If you have tried or learned Objective-C and the way the Apple apps work, there is an App Delegate, that will then initialise the UIWindow or load the NIB File.

waxClass{"AppDelegate", protocols = {"UIApplicationDelegate"}}

function applicationDidFinishLaunching(self, application)
  local frame = UIScreen:mainScreen():bounds()
  self.window = UIWindow:initWithFrame(frame)
  self.window:setBackgroundColor(UIColor:colorWithRed_green_blue_alpha(0.196, 0.725, 0.702, 1))

  local label = UILabel:initWithFrame(CGRect(0, 100, 320, 40))
  label:setFont(UIFont:boldSystemFontOfSize(30))
  label:setColor(UIColor:whiteColor())
  label:setBackgroundColor(UIColor:colorWithRed_green_blue_alpha(0.173, 0.651, 0.627, 1))
  label:setText("Hello Lua!")
  label:setTextAlignment(UITextAlignmentCenter)    
  self.window:addSubview(label)
  
  self.window:makeKeyAndVisible()
  
  puts("")
  puts("-------------------------------------------------")
  puts("- You can print stuff to the console like this! -")
  puts("-------------------------------------------------")  
end

This is the easiest of the lua bindings that you can create for your app. It can also be used to create Listview and other related controls, Apple uses protocols, namely the UITableViewDataSource and the UITableViewDelegate for UITableView object. Similarly for the Application, there is the UIApplicationDelegate protocol.

The function applicationDidFinishLaunching() is called after the app is loaded and ready for the user to populate it with stuff or load a NIB and get started. The window is created and a label saying Hello Lua is created by the above code.

As I was mentioning about delegates and protocols, the UIListview becomes an absolute pleasure to use with Lua bindings. You are aware that the functions numberOfSectionsInTableView is used to return the number of sections or groups in the UITableView,

function numberOfSectionsInTableView(self, tableView)
  return 1
 end

then there is the numberOfRowsinSection, which returns the number of items in the section/group specified by the passed parameter.
function tableView_numberOfRowsInSection(self, tableView, section)
  return 5
 end

perhaps the most interesting function of the whole lot is the cellForRowAtIndex which is defined as
function tableView_cellForRowAtIndex(self, tableView, indexPath
   local identifier = "mycell"
   local cell = tableView:dequeueReusableCellWithIdentifier(identifier) or
              UITableViewCell:initWithStyle_reuseIdentifier(UITableViewCellStyleDefault, identifier)

   cell:textLabel():setText("OZApps)
   cell:accessoryType(UITableViewCellAccessoryDiscolsureIndicator)

   return cell
  end

Some more example that Corey, the author of Wax has made on the website are

view = UIView:initWithFrame(GCRect(0,0,320,100))
view:setBackgroundColor(UIColor:redColor())

The best part is that the Lua arrays are Objective-C's Arrays and Dictionary equivalents.
images = {"myFace1.pn","myFace2.png, "myFace.png"}
imageView = UIImageView:initWithFrame(CGRect(0,0,320,460))


Good luck with that, so you can use xCode and Objective-C and build the apps that you want where as not getting bogged down with the extra stuff.

The only disadvantage of using Wax for development is that you cannot create builds for the Android. The advantage of this framework is that you can build for the Mac Desktop and/or for the iOS together.

Comments

  1. So... what is the main difference between Wax and Kobold2d ? Looks very similar..

    ReplyDelete
  2. Well first both use Wax as the lua binding mechanism.
    The difference is that the method above is using the UIKit, the native UI components, more so for Enterprise type apps where as Kobold2D is more so a wrapper on Cocos2D more so for gaming and OpenGL type stuff.

    If you follow Steffen, the author of Kobold2D, he has tried to compare different lua bindings too.

    ReplyDelete

Post a Comment

Popular Posts