Apr 18 2011

iPhone Unit Test tips: Read a file in your test

Category: bdd,objective-cgiordano scalzo @ 12:44 pm

Very often I need to unit test something read from a file, but it seems very difficult to accomplish this simple task in iOS, so usually I give up.
But today I felt was a good day to find a solution ;-)
In my code I want to read a JSon data from a file, so I wrote a simple test:

describe(@"Conference", ^{
    it(@"loads given a filename", ^{
        Conference *conference = [[Conference alloc]initWithFile:@"talks_with_three_tracks"];
        [[theValue([conference hasData]) should] beTrue];
    });
});

with a simple init method:

-(id)initWithFile:(NSString *)filename{
    if ((self = [super init])) {
        NSString* path = [[NSBundle mainBundle] pathForResource:filename ofType:@"json"];
        NSError *error;
        NSString* talksString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
        talks = [talksString JSONValue];
    }
    return self;
}

-(BOOL)hasData{
    return talks!=nil;
}

Unfortunately it doesn’t work :-( , because path is nil;
But here it isthe solution:
instead of using [NSBundle mainBundle], we should use the bundle associated with our class:

        NSString *path = [[NSBundle bundleForClass:[Conference class]] pathForResource:filename ofType:@"json"];

Simple and neat: thank you StackOverflow ;-)

Technorati Tags: , ,

Tags: , ,


Jul 09 2010

Simplified Ad Hoc Distribution in Xcode 3.2

Category: objective-cgiordano scalzo @ 11:41 am

Currently, Ad Hoc distribution is the only way to share an application to try itin to several devices or users (to maximum of 100 devices).
Despite the Apple’s reputation to create simple and effective things, the Ad Hoc Distribution is one of awkward and cumbersome process I’ve ever seen: I admit every time I need to change a certificate to add or remove certain devices or I’ve to check another application, I hold my breath and start cold sweat until the end of process… but, in Xcode 3.2 they simplified it a little bit.

In Build menu you can find a new entry, Build and Archive, that creates your application and archive it, allowing to share and signing it in a more confortable way:

All archived applications are in a new window of the Organizer:

When you “share” an application an ipa file is created and you can share it with your betatesters:

Just a little thing, but it’s useful to manage all versions.

More information can be found in this interesting post by Jeff LaMarche: if are serious about iPhone development you must subscribe his blog (and buy his book too ;-) )

Technorati Tags: , ,

Tags: , ,


Apr 02 2010

“Agile iPhone Development” at Xpug Milano

Category: me,objective-c,presentationgiordano scalzo @ 9:29 am

Spring is almost arriving and it’s time for another Xpug Milano meeting in coding dojo format.

This month we practiced with ObjectiveC; I gave an introductory presentation about the language, the ide and the framework, then we faced up to a nice Kata I proposed, inspired by some work of Brett Schuchert: the KataRpnCalculator.

I admit I’m amazingly surprised by how quick the Xpuggers learned the basic of the language and how good is the code they produced: awesome!
It’s the proof that it’s always a people problem not a technologic problem, and people with the right attitude can produce very good code, even though they don’t know very well the language.


Mar 17 2010

AgileCamp 2010: richness of difference

Category: agile,bdd,me,objective-c,presentationgiordano scalzo @ 2:59 pm

Some weeks ago, I has been lucky enough to attend to AgileCamp 2010, an awesome Barcamp organized by Sketchin, a Swiss UX and Web agency.
With my good fellows XpUg-gers Gabriele, Andrea and Indrit, we reached a lot of old and new friends, all of them caring about quality and “Things Got Right”.
I came back really enriched, mainly because I met people with experiences and point of view different than mine.

It has been my first Barcamp, so I though it was necessary to present something, and I decided to assemble a little introduction to Tdd coding for Iphone.

Here there is the video of my presentation:

AgileCamp – iPhone agile DEV (di Giordano Scalzo) from Sketchin on Vimeo.

Technorati Tags: , ,

Tags: , ,


Jan 19 2010

Objective-C for busy Java Developers 1: Calling methods

Category: java,objective-c,tutorialgiordano scalzo @ 2:34 pm

At last I got a wonderful MacBookPro, so I started to study Objective-C to develop some cool Iphone applications.

Objective-C is a language derived from C, to which it adds some modern features as ObjectOriented or Smalltalk-style messaging.

As far I’m a complete newbie, I’m trying to learn it recalling some well know patterns and scenarios as made in Java , following this good tutorial.

Objective-C has a little strange way to call method, that could be disorienting at first glance:

Java:

object.method;
object.methodWithInput(input);

output = object.methodWithOutput();
output = object.methodWithInputAndOutput(Object input);

Objective-C:

[object method];
[object methodWithInput:input];

output = [object methodWithOutput];
output = [object methodWithInputAndOutput:input];

Obviously, it’s possible to call methods of class, instead of instance:

Java:

Object oString = new String();

Objective-C:

id oString = [NSString string];

The

id

refers any kind of object, so it’s little different from Java counterpart.
Better code is:

Java:

String sString = new String();

Objective-C:

NSString* sString = [NSString string];

With this style, it’s a little cumbersome write nested calls:

Java:

calculator.add(numbers.split());

Objective-C:

[calculator add:[numbers split]];

This syntax disencourage the nesting of more than one method.

Some methods take multiple input arguments, Objective-C deals with that allowing split method names:

Java:

boolean writeToFile(String path, boolean useAuxiliaryFile)

boolean result = myData.writeToFile("/tmp/log.txt", false);

Objective-C:

-(BOOL)writeToFile:(NSString *)path withAuxFile:(BOOL)useAuxiliaryFile;

BOOL result = [myData writeToFile:@"/tmp/log.txt" withAuxFile:NO];

Objective-C has properties built in, in Java you need to implement getters and setters:

Java:

photo.setCaption("Day at the Beach");
output = photo.getCaption();

Objective-C:

photo.caption = @"Day at the Beach";
output = photo.caption;

A property should be marked

@property

in declaration and

@synthesize

in implementation.

To create an object, the function

alloc

should be called and then an init method should be called:

Java:

object = new ComplexObject(1.0f);

Objective-C:

object = [[ComplexObject alloc] initWithFloat:1.0f];

When working in an environment without garbage collector, any object created with alloc should be released:

Objective-C:

[object release];

To complete this introductory post, take a look at this ObjectiveC CheatSheet: it contains all the most used constructs needed to start to code for Mac.

Technorati Tags: , ,

Tags: , ,