Celedev Blog Archive

Old posts that were originally published on celedev.com

Celedev CodeFlow has now been renamed Bounces.

Some posts in this section may be outdated or not relevant anymore.
Please refer to the Home page and the Documentation section for current information about Bounces.

CodeFlow 0.9.9: the power of structs


0.9.9 is a great version number for a software. And this is precisely the version number of the new CodeFlow beta, available since yesterday in the downloads section of celedev website.

What is new in this version?

  • Major improvements in the use of C structs from the Lua code: CodeFlow now supports an object-oriented syntax for structs, that makes the code more concise and readable, and enables you to very easily define your own struct methods. This new feature is described in more details below in this post.

  • A better bindings libraries management adapted to the various iOS SDK versions available on the host computer. In particular, CodeFlow can now automatically select the default SDK corresponding to the version of Xcode installed on your development machine. And if you select a specific SDK for your CodeFlow project, CodeFlow will open the associated Xcode project with the right Xcode version for this SDK (on OS X Yosemite only). Really convenient for testing projects that use a beta iOS SDK version!

  • The fix of several problems that could make CodeFlow crash under OS X 10.10 Yosemite.

  • And of course many smaller improvements and bug fixes.

CodeFlow Bindings libraries adopt a new format with this version and need to be updated. In addition, CodeFlow Bindings for iOS 8.2 SDK have been upgraded to match the beta 2 SDK version released by Apple a few days ago.

A new version 0.9.9 of the CodeFlow Sample Applications Package is available too. It includes a new application WatchApp1 that shows how to use CodeFlow to develop an Apple Watch app, and updates of existing applications to illustrate the use of structs with the new object syntax.

[Edit 18 Dec. 2014 22:45 CET] As Apple has just released Xcode 6.2 beta 3, CodeFlow Bindings for iOS 8.2 SDK have been updated to match the iOS 8.2 beta 3 SDK. So has the WatchApp1 CodeFlow sample application, which is now compatible with API changes in WatchKit in iOS 8.2 beta 3.

Using structs in Codeflow

Structs are used heavily in C / Objective-C APIs and the iOS SDK is no exception. Data of struct types, like NSRange or CGRect, are used as parameter or return value in many functions and methods of the SDK. To make the use of these functions/methods possible and practical from the Lua code, CodeFlow supports, since many versions, the transparent conversion from a Lua table to a struct parameter, as well as the reading or writing of struct data fields from Lua.

For example:

local myView = self.view -- self.view is a UIView

-- set a struct value with a Lua table
myView.center = { x = 100, y = 256 }

-- use a struct value in Lua
local center = myView.center -- center is a reference to a C struct
center.x = 50 -- set a struct field
print ("The center is (" .. center.x .. ", " .. center.y .. ")") -- get struct fields

In addition to this basic support, CodeFlow 0.9.9 adds two new tools: struct constructors and struct methods: constructors allow the creation in Lua of a struct with a given type, without necessarily creating it from a Lua table; struct methods provide an easy way to manipulate structs or to get information from them, using a unified syntax with objects, and as a result, improving the code consistency and readability.

All struct types known by the program are stores in a global Lua table struct, that provides a specific namespace for struct types and avoids collision with other Lua global variables. The list of types in the struct table can vary from program to program, as it depends of the frameworks used by the CodeFlow project. For example, in the WatchApp1 application, that uses MapKit, Foundation and CoreGraphics, struct contains the following types:

Struct types in the WatchApp1 app

Struct constructors are used like this:

local CGRect = struct.CGRect -- using a local variable makes the use of CGRect more concise and improves performance in the rest of the code

-- create a CGPoint value with the list of its fields
local point1 = struct.CGPoint (0, 150)

-- create a  CGSize value with a Lua table
local size1 = struct.CGSize {width = 200, height = 100 }

-- create a CGRect value with the list of its fields
local rect1 = CGRect (point1, size1)

-- create a CGRect value with the list of its sub-fields
local rect2 = CGRect (50, 65, size1.width, 500)

-- create an empty CGRect (with all fields set to zero)
local rect3 = CGRect()
rect3.origin.x = 10
rect3.origin.y = 15
rect3.size = rect2.Size

Struct methods can be defined by Bindings Libraries or by your Lua code. Two standard methods are defined for every struct type:

  • copy() that duplicates a struct value
  • hasType(someStructType) that tests if a struct value really has the expected type

And as everything in CodeFlow is compatible with live-coding, adding or replacing a struct method can be done at any time, including in live mode while the app is running.

Here are a few examples of using and defining method structs in Lua:

-- this uses local variables from the previous code sample...

-- duplicate a struct value
local point2 = point1:copy()
point2.x = 10 -- this does not modify point1

-- add a method to CGRect
function CGRect:centeredRectWithSize (width, height)
    -- if the method is called with a single parameter, return a square
    height = height or width
    -- create the centered rectangle (note the use of predefined CGRect methods getMidX and getMidY)
    return CGRect (self:getMidX() - width / 2, self:getMidY() - height / 2, width, height)
end

if rect1:hasType(CGRect) then
    local rect4 = rect1:CGRect:centeredRectWithSize(50)
    -- use rect4...
end

You can visualize existing struct methods in CodeFlow Variable Inspector while the app is running, by unfolding a type in the struct global variable:

CGRect methods in CodeFlow Variable Inspector

We can notice here the presence of the centeredRectWithSize method defined in the Lua code (with icon Lua function icon) ; other methods are C functions (with icon C function icon) defined in Bindings Libraries.

Et voilà! This is about all you need to know to use structs in CodeFlow. Quite simple, right?

In any case, as a reminder, CodeFlow beta can be downloaded for free. So do not hesitate to give it a try and to send us your feedback or suggestions, either in the comments of this post, or by sending an email to support@celedev.com.

Post a Comment


Recent posts

Blog Post
Aug 1, 2016

CodeFlow 1.0.2

CodeFlow 1.0.2 is a minor release that focuses on improving the Live Application Developer's Experience.

Aug 1, 2016
Blog Post
Jun 16, 2016

CodeFlow 1.0.1 and WWDC 2016

The just-released CodeFlow 1.0.1 brings support for the new iOS 10, tvOS 10 and macOS 10.12 announced at WWDC 2016 this week.

Jun 16, 2016
Blog Post
Jun 9, 2016

CodeFlow turns 1.0

It has been some time since the last beta of CodeFlow, version 0.9.20 was released in January this year. And all this time, we have worked very hard to improve CodeFlow, and to turn it into an effective Application Development System that we love to…

Jun 9, 2016
Blog Post
Apr 22, 2016

Live storyboards in CodeFlow

Live storyboards are a important feature of the upcoming CodeFlow 1.0. Mixing the power of Xcode storyboards with the flexibility of CodeFlow live coding, they are amazing for fast, fun and creative live app development.

Apr 22, 2016