`

Lua vs. Obj-C

    博客分类:
  • lua
 
阅读更多

Lua

For some time now I’ve been digging into Lua. Coming from Python, the power of a clean syntax and good documentation is impossible to ignore. 
Lua is a powerful, fast, lightweight, embeddable scripting language. 

Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, runs by interpreting bytecode for a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping.

It’s that exotic, super-fast scripting language that very few use, right? Partially right. It’s extensively used in the gaming industry (e.g. World of Warcraft) usually together with C++. 

Lua comes with a console, which is great tool for fast experimenting of logic and syntax.



 All fine and dandy, but how can this help me on iOS development? Stefan found the right answer: iPhone Wax .

Wax is a framework that lets you write native iPhone apps in Lua. 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!” 
Corey Johnson, main guy behind Wax

As anyone with iOS development experience knows, Obj-C is very strict about classes, requiring a certain mindset to write good, maintainable and modular applications. Wax doesn’t alter this approach at all. 

Memory management is another worry that goes away in Wax, because of the automatic garbage collection. 

By this point, Wax looked completely different than any other write-quick-iphone-appsgimicks out there, including PhoneGap, Titanium, Rhomobile, Corona SDK. Do note that I mention “iPhone apps”, since the result of Wax is not cross platform. Well, a solid application should take advatage of the hardware platform and the only part that can be cross platform is the interface, even that within certain limits.

Cocoa with a sprinkle of Lua

Wax looked good in theory. A UITabBarController is a UITabBarController, a delegate is a delegate, 3rd party libs work how they are intended to (FacebookASIHTTPRequest, custom written ones). Now let’s see it in practice. 

I gave Wax a run-around for 1 week. During that time I tried using most of the UITouch, Cocoa elements and some 3rd party libraries. The results were good. The interface is just as responsive as building directly in Obj-C. 

After that test week, the decision of using Wax for the rest of app development seemed obvious, even more, Wax seems like a valuable addition to the development cycle.

Side-by-side example and benchmark

Here’s a small benchmark of memory usage by the same application, written in Wax and in Obj-C. The application used is Fortune Crunch from Mark’s tutorial on MobileTuts+
Objective-C code (can be seen in the source code of the tutorial too):

// FortuneCrunchAppDelegate.h
//
#import <UIKit/UIKit.h>

@class FortuneCrunchViewController;

@interface FortuneCrunchAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    FortuneCrunchViewController *viewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet FortuneCrunchViewController *viewController;

@end



// FortuneCrunchAppDelegate.m
//
#import "FortuneCrunchAppDelegate.h"
#import "FortuneCrunchViewController.h"

@implementation FortuneCrunchAppDelegate

@synthesize window;
@synthesize viewController;


#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after application launch.

    // Add the view controller's view to the window and display.
    [self.window addSubview:viewController.view];
    [self.window makeKeyAndVisible];

    return YES;
}

- (void)dealloc {
    [viewController release];
    [window release];
    [super dealloc];
}


@end



// FortuneCrunchViewController.h
//

#import <UIKit/UIKit.h>

@interface FortuneCrunchViewController : UIViewController {

	IBOutlet UIButton *fortuneCookieButton;
	IBOutlet UILabel  *fortuneLabel;
	
}

@property(nonatomic, retain) IBOutlet UIButton *fortuneCookieButton;
@property(nonatomic, retain) IBOutlet UILabel *fortuneLabel;

-(IBAction)crunchCookie:(id)sender;

@end


 
// FortuneCrunchViewController.m
//
#import "FortuneCrunchViewController.h"

@implementation FortuneCrunchViewController

@synthesize fortuneCookieButton;
@synthesize fortuneLabel;

// This method changes the cookie image when the button is pressed:

-(IBAction)crunchCookie:(id)sender {
	NSLog(@"In crunchCookie");
	[fortuneCookieButton setImage:[UIImage imageNamed:@"cookie-crunched.png"]
						 forState:UIControlStateNormal];
	
	fortuneLabel.hidden = NO;
	
}

// These methods are related to memory management:

- (void)viewDidUnload {
    [fortuneCookieButton release];
	fortuneCookieButton = nil;
}

-(void)dealloc {
	[fortuneCookieButton release];
	[fortuneLabel release];
	[super dealloc];
}

@end
 -- AppDelegate.lua
waxClass{"AppDelegate", protocols = {"UIApplicationDelegate"}}

require "FortuneCrunchViewController"

function applicationDidFinishLaunching(self, application)
  local frame = UIScreen:mainScreen():bounds()
  self.window = UIWindow:initWithFrame(frame)
  self.window:setBackgroundColor(UIColor:whiteColor())
  local fc = FortuneCrunchViewController:init()    
  self.window:addSubview(fc:view())  
  self.window:makeKeyAndVisible()
end

-- FortuneCrunchViewController.lua
waxClass{"FortuneCrunchViewController", UIViewController}

function crunchCookie(self, sender)
	self.button = self:view():viewWithTag(1)
	self.button:setImage_forState(UIImage:imageNamed('cookie-crunched.png'), UIControlStateNormal)
	self.label = self:view():viewWithTag(2)
	self.label:setHidden(false)
end

function init(self)
  self.super:initWithNibName_bundle("FortuneCrunchViewController", nil)
  return self
end

 Both projects use the same .xib created in Interface Builder, per the tutorial. Bellow are 2 screen-shots of the memory usage. 

Objective-C



 Wax/Lua



 As expected, Wax has a small foot-print.

Conclusion

Most of the people dislike the syntax of Obj-C and it does slow down development. Speed and maintainability are of great value for new apps and here is an awesome alternative!

  • 大小: 40.8 KB
  • 大小: 148.4 KB
  • 大小: 144.7 KB
分享到:
评论

相关推荐

    编译lua并且在VS中配置lua

    标题“编译lua并且在VS中配置lua”涉及的核心知识点是关于如何在Visual Studio (VS)环境下构建和配置Lua脚本语言的开发环境。这里主要分为两个步骤:编译Lua源代码以及在VS中设置相应的配置。 首先,我们来看编译...

    转载:已编译好的openssh库

    msvcrt.lib(MSVCRT.dll) : error LNK2005: _strchr already defined in libcmtd.lib(strchr.obj) ... 这是由于OpenSSL的静态函数库使用的是了VC的多线程DLL的Release版本,而我的程序使用了多线程静态链接的...

    Lua53 vs sln 完整编译配置工程.zip

    为了编译Lua53,我们需要确保VS环境中包含了C编译器,因为Lua是用C语言编写的。同时,需要设置正确的编译选项,比如启用C89兼容模式,因为Lua的源码遵循这个标准。此外,可能还需要设置一些额外的宏定义,如LUA_...

    Lua快速入门指南.pdf

    #### 一、Lua与C/C++的不同之处 Lua是一种轻量级的、高效的脚本语言,特别适用于嵌入到应用程序中,以提供灵活的配置和扩展能力。对于C/C++程序员而言,理解Lua的独特之处非常重要。尽管两者在某些语法上存在相似性...

    CC++程序员的Lua快速入门指南.pdf

    2. **字符串(String)**: Lua中的字符串是一种特殊的数据类型,与C语言中的字符串不同。Lua字符串总是以零结尾,但可以包含任何字符,甚至是零字符本身。这意味着Lua字符串是C字符串的一个超集。 3. **布尔...

    OC与Lua交互调用(不使用第三方库)Demo

    在iOS开发中,Objective-C(OC)和Lua的交互是一个常见的需求,特别是在游戏开发中,因为Lua具有轻量级、脚本化的特性,适合用于处理游戏逻辑和动态内容。本Demo提供了一种不依赖第三方库的OC与Lua交互方式,通过...

    C/C++的Lua快速入门

    ### C/C++程序员Lua快速入门知识点详解 #### 数据类型 Lua的数据类型分为八种基本类型,这些类型在很多方面与C/C++有所不同。 1. **数值(Number)**:内部以`double`形式存储,这使得Lua能处理浮点运算。尽管...

    C和C++程序员的Lua快速入门指南

    ### C和C++程序员的Lua快速入门指南 #### 前言 对于有着C/C++背景的程序员来说,转向Lua可能会遇到一些全新的概念和技术。本文旨在帮助这些程序员快速掌握Lua的核心特性和编程模式,以便他们能够顺利过渡到Lua的...

    Lua快速入门 pdf

    - **函数(function)**:Lua的核心概念之一,不同于C语言中的函数或函数指针。 - **表(table)**:一种“异构”的哈希表,是Lua的另一个关键概念。 - **userdata**:C语言用户定义的数据结构,用于扩展Lua语言。 - **...

    Lua 5.1 manual 笔记

    ### Lua 5.1 手册笔记概览 本文档主要根据 FinixLei 整理的 Lua 5.1 手册笔记进行提炼与总结,重点在于介绍 Lua 的基本数据类型及其用法。 #### 基本数据类型概述 在 Lua 中,存在多种基本的数据类型,包括 `nil`...

    lua Tinker 0.5c (Visual Studio 2008编译)

    Lua Tinker 0.5c 是一个基于 Lua 脚本语言的库,它为 C++ 开发者提供了一个简洁的接口,以便于在 C++ 应用程序中嵌入和使用 Lua。这个版本是特别针对 Visual Studio 2008 编译环境构建的,意味着你可以直接在该...

    LuaBind 源码 (Lua增强库)

    extern "C" int luaopen_luabind_test(lua_State* L) { using namespace luabind; open(L); module(L) [ def("greet", &greet) ]; return 0; } 把生成的DLL和lua.exe/lua51.dll放在同一个目录下. Lua 5.1.2 ...

    Lua快速入门

    2. **字符串(string)**:与C语言中的字符串不同,Lua字符串以零结尾,但也可以包含零字符,因此是C字符串的超集。 3. **布尔(boolean)**:仅包含`true`和`false`两个值。 4. **函数(function)**:Lua的一个...

    VS2012+LUA5.2+boost1.47.0环境搭建方法

    4. 集成LUA和luabind:将LUA源码解压,将`src`目录下的所有.h和.c文件复制到你的项目中。同样,将luabind生成的库文件(通常是libluabind.lib)和头文件(在luabind的include目录下)添加到你的项目中。 5. 配置...

    lua通过so调用C++类

    为了在Lua中调用C++函数,我们需要使用Lua的C API,如`luaL_loadbufferx`和`lua_pcall`。首先,我们需要加载SO文件并获取导出函数的地址,这通常通过`dlopen`和`dlsym`函数完成。 ```c #include &lt;dlfcn.h&gt; void* ...

    C和C++程序员的Lua快速入门

    ### C和C++程序员的Lua快速入门 #### 数据类型与基本概念 Lua作为一种脚本语言,在数据类型上与C/C++存在明显差异。对于有经验的C/C++开发者来说,理解这些不同之处至关重要。 **八种基本类型:** 1. **数值...

    lua 源代码 添加了 cJson

    在这个“lua 源代码 添加了 cJson”的项目中,开发者整合了Luaj,一个允许Java应用程序运行Lua脚本的库,与cJson,一个高效的C语言实现的JSON解析器和序列化器。这样的结合对于Android应用尤其有用,因为它可以在...

Global site tag (gtag.js) - Google Analytics