Objectice-C 2.0 - AppStore tutorial

About this tutorial

Objective-C Directive has been released since Anycode 1.2.0+ . I must admit that i'm not very familiar with this language. Objective-C support is therefore limited. However, i'll be glad to take into account any of your suggestions and advices for future releases of the tool.

All the source code described in this tutorial is available on anycode tutorials bitbucket repository

UML Model

For this example, we consider a very (very) simple App Store model :

Objective-C Interface template

Objective-C Class consists in 2 files : an interface definition and its implementation. Let's write interface template :




#import <Foundation/Foundation.h>


@interface ${c.name} : NSObject {
	<% c.attributes.each { %>
	<% } %>
<% c.attributes.each { %>
	@property ${it.dataType.primitive ? '(assign)' : '(retain)'} ${objc.attribute(it)}
<% } %>

<% c.operations.each { %>
<% } %>

Objective C Directive

As you can see in the above example, 'objc' variable is heavily used. This is ObjectiveC Directive class. For more details about this directive, please see Objective C Directive JavaDoc API

Automatic Class import

For compiling reasons, an interface that references another class has to declare it with @class keyword. This is what classImports method does.

Attributes, getters and setters

Since Objective 2.0, getters and setters can be declared in interface using @property keyword. Again we can use Objective C directive to render attributes and relations. It will automatically detect primitive types and turn them into valid ObjectiveC types. Collections will be mapped as NSArray.

Objective-C Implementation template

Implementation template is even easier than interface's.




#import "${c.name}.h"
@implementation ${c.name}
<% c.attributes.each { %>
	@synthesize ${objc.getAttributeName(it)};
<% } %>

<% c.operations.each { %>
<% } %>

Well that's it. This will generate your classes, their relations and methods (with empty implementation).

Testing your source code

The sources of this tutorial are available online here. It contains a the templates, generated code and a running main class that shows how to use classes.