<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>stiefels.net &#187; objective-c</title>
	<atom:link href="http://www.stiefels.net/tag/objective-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.stiefels.net</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Sun, 20 Sep 2009 10:39:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Getting Started with iPhone Development</title>
		<link>http://www.stiefels.net/2008/11/23/getting-started-with-iphone-development/</link>
		<comments>http://www.stiefels.net/2008/11/23/getting-started-with-iphone-development/#comments</comments>
		<pubDate>Sun, 23 Nov 2008 16:26:39 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[SDK]]></category>

		<guid isPermaLink="false">http://www.stiefels.net/2008/11/23/getting-started-with-iphone-development/</guid>
		<description><![CDATA[This weekend I finally found some time to watch [Bill Dudney](http://bill.dudney.net/roller/bill/)'s screencasts about iPhone development.
The screencast series is called '[Writing your first iPhone Application](http://pragprog.com/screencasts/v-bdiphone/writing-your-first-iphone-application)' and it is an ideal way to get started with iPhone application development.
All you need is a recent version of Xcode and some basic knowledge about Objective-C and Cocoa.
In 2,5 hours [...]]]></description>
			<content:encoded><![CDATA[<p>This weekend I finally found some time to watch [Bill Dudney](http://bill.dudney.net/roller/bill/)'s screencasts about iPhone development.<br />
The screencast series is called '[Writing your first iPhone Application](http://pragprog.com/screencasts/v-bdiphone/writing-your-first-iphone-application)' and it is an ideal way to get started with iPhone application development.<br />
All you need is a recent version of Xcode and some basic knowledge about Objective-C and Cocoa.<br />
In 2,5 hours Bill shows you how to create a simple but nice recipe application where you can store recipes and its ingredients (sure, it is NOT a full-blown recipe database application but it serves its purpose). You learn a lot about table views, view controllers, navigation controllers and how to store your entered data persistently by using [SQLite](http://sqlite.org/).</p>
<p>I really enjoyed watching it and I'd recommend to write the application in parallel while you are watching it. That way you'll think more about how all the stuff works.<br />
Every episode (five in total) costs $5 but I think it's completely worth the money.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stiefels.net/2008/11/23/getting-started-with-iphone-development/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Full Perl-Compatible Regular Expressions in Objective-C</title>
		<link>http://www.stiefels.net/2007/09/10/full-perl-compatible-regular-expressions-in-objective-c/</link>
		<comments>http://www.stiefels.net/2007/09/10/full-perl-compatible-regular-expressions-in-objective-c/#comments</comments>
		<pubDate>Mon, 10 Sep 2007 19:20:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://www.stiefels.net/2007/09/10/full-perl-compatible-regular-expressions-in-objective-c/</guid>
		<description><![CDATA[A few months ago I wrote about a mechanism which allowed simple [Pattern Matching with NSPredicate](http://www.stiefels.net/2007/01/24/regular-expressions-for-nsstring/).
Although it is a good method to check if a string matches a Regular Expression it lacks a lot of features you normally expect while you're doing something with Regular Expressions, such as capturing subpatterns or doing search and replace.
[Philip [...]]]></description>
			<content:encoded><![CDATA[<p>A few months ago I wrote about a mechanism which allowed simple [Pattern Matching with NSPredicate](http://www.stiefels.net/2007/01/24/regular-expressions-for-nsstring/).<br />
Although it is a good method to check if a string matches a Regular Expression it lacks a lot of features you normally expect while you're doing something with Regular Expressions, such as capturing subpatterns or doing search and replace.</p>
<p>[Philip Hazel](http://www.uit.co.uk/exim-book/author-bio.htm) (who also wrote my favourite SMTP server, [Exim](http://www.exim.org) created a library called [PCRE (Perl-Compatible Regular Expressions)](http://www.pcre.org) a long time ago which implements the Regular Expression syntax used in [Perl](http://www.uit.co.uk/exim-book/author-bio.htm) (sometimes I miss that time) for the C Programming Language.<br />
While PCRE is great for doing Regular Expressions in C, it's usage with Objective-C is some kind of cumbersome as NSString objects must be converted to plain C strings and back again.</p>
<p>But recently John Engelhart released an alpha version of his PCRE-based [RegexKit framework](http://regexkit.sourceforge.net), which provides a sweet object-oriented interface to Regular Expressions. It looks very mature and there is also a [very good documentation](http://regexkit.sourceforge.net/Documentation/index.html) available.</p>
<p>Thank you very much, John!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stiefels.net/2007/09/10/full-perl-compatible-regular-expressions-in-objective-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>STNPostgreSQL.framework Universal Binary and Examples</title>
		<link>http://www.stiefels.net/2007/05/31/stnpostgresqlframework-universal-binary-and-examples/</link>
		<comments>http://www.stiefels.net/2007/05/31/stnpostgresqlframework-universal-binary-and-examples/#comments</comments>
		<pubDate>Thu, 31 May 2007 10:04:43 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://www.stiefels.net/2007/05/31/stnpostgresqlframework-universal-binary-and-examples/</guid>
		<description><![CDATA[I updated the [STNPostgreSQL.framework project page](/projects/stnpostgresqlframework) with a download link to a Universal Binary of the framework and a few examples on how to use it.
]]></description>
			<content:encoded><![CDATA[<p>I updated the [STNPostgreSQL.framework project page](/projects/stnpostgresqlframework) with a download link to a Universal Binary of the framework and a few examples on how to use it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stiefels.net/2007/05/31/stnpostgresqlframework-universal-binary-and-examples/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Preview: STNPostgreSQL.framework</title>
		<link>http://www.stiefels.net/2007/05/24/preview-stnpostgresqlframework/</link>
		<comments>http://www.stiefels.net/2007/05/24/preview-stnpostgresqlframework/#comments</comments>
		<pubDate>Thu, 24 May 2007 11:11:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://www.stiefels.net/2007/05/24/preview-stnpostgresqlframework/</guid>
		<description><![CDATA[I am very pleased to announce the public preview of STNPostgreSQL.framework.
STNPostgreSQL.framework is an open source Objective-C framework for accessing [PostgreSQL](http://www.postgresql.org) databases in a convenient object oriented way.
The current version is **pre-beta** so don't expect that much features or stability. The APIs are still subject to change!
I have a lot of ideas which will be released [...]]]></description>
			<content:encoded><![CDATA[<p>I am very pleased to announce the public preview of STNPostgreSQL.framework.<br />
STNPostgreSQL.framework is an open source Objective-C framework for accessing [PostgreSQL](http://www.postgresql.org) databases in a convenient object oriented way.</p>
<p>The current version is **pre-beta** so don't expect that much features or stability. The APIs are still subject to change!</p>
<p>I have a lot of ideas which will be released in future versions. If you have some questions, ideas for improvement or if you want to join me on this project please send me a [mail](/contact). Thanks!</p>
<p>More information and download instructions can be found on the [project page](/projects/stnpostgresqlframework).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stiefels.net/2007/05/24/preview-stnpostgresqlframework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unit Testing with Xcode</title>
		<link>http://www.stiefels.net/2007/05/01/unit-testing-with-xcode/</link>
		<comments>http://www.stiefels.net/2007/05/01/unit-testing-with-xcode/#comments</comments>
		<pubDate>Tue, 01 May 2007 21:26:21 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.stiefels.net/2007/05/02/unit-testing-with-xcode/</guid>
		<description><![CDATA[Unit testing is some kind of a controversial topic. There are many opinions by software developers whether to make unit tests and how frequently they should be used.
I don't want to start a preach here or discuss the pros and cons of unit tests in every little detail. It's just a little tutorial how Unit [...]]]></description>
			<content:encoded><![CDATA[<p>Unit testing is some kind of a controversial topic. There are many opinions by software developers whether to make unit tests and how frequently they should be used.<br />
I don't want to start a preach here or discuss the pros and cons of unit tests in every little detail. It's just a little tutorial how Unit Testing works with Objective-C and Xcode.</p>
<p>Software needs to be tested, right? So, when is the right time to test your application?<br />
Well, before I heard something about Unit Tests I did tests every time after I changed a function or added some functionality to my software and I think this is what most people do.<br />
The problem is that mostly after these changes only a few parts of the application are tested. The application is/can never tested completely by the developer by hand and errors might occur when a end user uses it.<br />
So the ideal test would be to go all through your software and test every little feature and functionality. I think that is not really possible. But this process can be automated. And at this point Unit Tests come into the play.</p>
<p><span id="more-44"></span></p>
<p>Unit Tests are used to validate components of your software (e.g. classes) during the development cycle. If written in parallel to the actual application code these tests can be used to validate your code immediately after it is compiled.<br />
This ensures that your code is working as expected, if your tests succeed. Also, you'll notice very soon if something is broken and this can save you a lot of time to locate the error(s).<br />
The more tests, the more functionality can be tested, but also the time effort to write these tests increases. Time which could have been used to write actual code. But on the other hand it could be possible, that one small additional test could save you hours of debugging in the future.<br />
It is important to find the right tradeoff between the time which is invested in writing tests and the usefulness of this test.</p>
<p>How much tests are created is the decision of each individual developer but there are a few reasonable cases which should be considered.<br />
For example, if a method does some calculations it would be nice to know how this method reacts to particular values such as extreme values or the null value.<br />
In Apples documentation I found a nice rule of thumb: One test is better than none. <img src='http://www.stiefels.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>In this arcticle I want to show how Unit Tests can be included in your software development cycle.<br />
I want to do this in a tutorial like style, so you can follow the steps easily. Although this tutorial sticks to Objective-C and the SenTestingKit Unit Tests are also available to a wide range of programming languages since it is a general concept of software development.</p>
<p>So far so good. Let's get started.</p>
<p>#### The fraction class ####</p>
<p>Our tutorial project is a small class which represents a mathematical fraction.<br />
One thing we want to do is that we will create the Unit Tests before we start creating the actual project code. This approach is called **Test-Driven-Development** as your tests describe what the class have to do.</p>
<p>In a UML-like class diagramm it looks like this:</p>
<p><img src="http://www.stiefels.net/wp-content/uploads/2007/05/fraction-uml.png" height="180" width="300" border="0" align="middle" hspace="4" vspace="4" alt="UML diagram" title="UML diagram" /></p>
<p>#### Creating a Xcode project ####</p>
<p>At this point we should create our Xcode project, but we won't write any fraction-class code yet.<br />
Start Xcode, select *New project...* from the *File* menu and as we just have a simple class it is sufficient to create a command line tool.<br />
Select *Foundation Tool* within the *Command Line Utility* section and name it *UTFraction*.<br />
This will create one Objective-C file containing the main function.</p>
<p><img src="http://www.stiefels.net/wp-content/uploads/2007/05/Projekt-Typ.png" height="249" width="300" border="0" align="middle" hspace="4" vspace="4" alt="new project" title="new project" /></p>
<p><img src="http://www.stiefels.net/wp-content/uploads/2007/05/project-name.png" height="249" width="300" border="0" align="middle" hspace="4" vspace="4" alt="project name" title="project name" /></p>
<p>#### What to test? ####</p>
<p>Writing tests for the getters and setters is boring and does not make very much sense.<br />
But we have two interesting methods in our class which we should test: *cancelDown* and *greatestCommonDivisorOf:and:*.<br />
And what exaclty do we want to test?</p>
<p>##### cancelDown: #####<br />
1. Successful canceling with numerator = 10, denominator = 5; expected result = 2/1<br />
2. Unsuccessful canceling with numerator = 9, denominator = 5; expected result = 9/5<br />
3. Canceling down with numerator = 0, denominator = 5; expected result: 0/5</p>
<p>##### greatestCommonDivisorOf:and: #####<br />
4. GCD not equal 1: numerator = 119, denominator = 14; expected result = 7<br />
5. Numerator zero: numerator = 0, denominator = 11; expected result = 1</p>
<p>Above I mentioned that testing the getters and setters is useless. Well, except that setting the denominator to zero should be prohibited by throwing an exception because a fraction with a denominator value of zero is not defined.</p>
<p>##### setDenominator: #####<br />
6. throws exception if parameter is zero.</p>
<p>As we now have defined what tests we want to implement we can start writing them.</p>
<p>#### Adding the Unit Test target ####</p>
<p>Adding Unit Tests to a project is a very simple procedure as you just have to add another target to your Xcode project.<br />
To do this click on the gearwheel and select *New Target*.</p>
<p><img src="http://www.stiefels.net/wp-content/uploads/2007/05/new-target.png" height="139" width="250" border="0" align="middle" hspace="4" vspace="4" alt="new target" title="new target" /></p>
<p>An assistant window appears. Select *Unit Test Bundle* in the *Cocoa* section and name it *Unit Tests*.</p>
<p><img src="http://www.stiefels.net/wp-content/uploads/2007/05/unit-test-bundle.png" height="249" width="300" border="0" hspace="4" vspace="4" alt="selecting unit test bundle" title="selecting unit test bundle" /></p>
<p><img src="http://www.stiefels.net/wp-content/uploads/2007/05/target-name.png" height="249" width="300" border="0" align="middle" hspace="4" vspace="4" alt="target-name" title="target-name" /></p>
<p>Close the upcoming Target Info window. You'll notice that the new target appears in the targets section within the *Groups &#038; Files* column.</p>
<p><img src="http://www.stiefels.net/wp-content/uploads/2007/05/groups-and-files.png" height="141" width="206" border="0" align="middle" hspace="4" vspace="4" alt="groups and files" title="groups and files" /></p>
<p>#### Writing the tests ####</p>
<p>Unit tests are organized in classes, so called test case classes.<br />
Within these classes test cases are defined. Each method of a test case class represents a test case. Usually, a test case method implements one specific test case but it is also possible to create complex test cases within one method.<br />
To add a test case class to your Unit Test target, open the *File* menu and select *New File...*. Select the *Objective-C test case class* entry within the *Cocoa* section.</p>
<p><img src="http://www.stiefels.net/wp-content/uploads/2007/05/test-case-class.png" height="249" width="300" border="0" align="middle" hspace="4" vspace="4" alt="test case class" title="test case class" /></p>
<p>Name it *FractionTests*. Please note that the corresponding target is *Unit Tests* so be sure the right checkbox is set.</p>
<p><img src="http://www.stiefels.net/wp-content/uploads/2007/05/test-class-name.png" height="249" width="300" border="0" align="middle" hspace="4" vspace="4" alt="test-class name" title="test-class name" /></p>
<p>A test case class is organized in three parts: setup, teardown and tests.</p>
<p>In the setup, preparing work is done, such as object instantiation or variable definitions. Setup is automatically called before each test case is executed.<br />
In our setup method we will instantiate one Fraction object and set some default values.</p>
<p>In the test class header file (FractionTests.h), we will add a attribute named *fraction* of the class *Fraction* (which does not exist yet).</p>
<p>    #import <senTestingKit/SenTestingKit.h><br />
    #import "Fraction.h"</p>
<p>    @interface FractionTests : SenTestCase {<br />
        Fraction *fraction;<br />
    }</p>
<p>    @end</p>
<p>In the implemetation file (FractionTests.m) we start writing the setUp method:</p>
<p>    - (void)setUp<br />
    {<br />
        fraction = [[Fraction alloc] initWithNumerator:1<br />
                                           denominator:1];<br />
    }</p>
<p>The teardown method is the complement to the setup method and is used to clean up the memory i.e. by releasing objects.<br />
As you already might think this method is automatically called after each test case.</p>
<p>    - (void)tearDown<br />
    {<br />
        [fraction release];<br />
    }</p>
<p>Now we come to the part where we implement the tests we considered above. Let's start with the first one.</p>
<p>    - (void)testSuccessfulCanceling<br />
    {<br />
        [fraction setNumerator:10];<br />
        [fraction setDenominator:5];<br />
        [fraction cancelDown];<br />
        STAssertTrue([fraction numerator] == 2,<br />
          @"Numerator != 2 (is %d)", [fraction numerator]);</p>
<p>        STAssertTrue([fraction denominator] == 1,<br />
          @"Denominator != 1 (is %d)", [fraction denominator]);<br />
    }</p>
<p>All test case methods have to begin with *test* in the method name. This ensures that this method is called automatically by the test case class.<br />
You can also add some other methods but they are not called automatically.<br />
The first step is to set our fraction object attributes.<br />
After that we call our cancelDown: method which will (if implemented correctly) cancel down our fraction. To check whether this worked we use one of the SenTestingKit macros.<br />
The *STAssertTrue*-macro checks whether the expression (first parameter) is true. If so nothing happens, but if the expression is false, an error message appears (second parameters).</p>
<p>These macros have to be used to tell the test case class if this test case worked or not. There are many macros you can use but there is only a small number of commonly used ones:</p>
<p>    STAssertNotNil(a1, description, ...)<br />
    STAssertTrue(expression, description, ...)<br />
    STAssertFalse(expression, description, ...)<br />
    STAssertEqualObjects(a1, a2, description, ...)<br />
    STAssertEquals(a1, a2, description, ...)<br />
    STAssertThrows(expression, description, ...)<br />
    STAssertNoThrow(expression, description, ...)<br />
    STFail(description, ...)</p>
<p>The *description* parameter is a formatable NSString which is shown as error message if the test fails. It is adviseable to display the current value of the object tested beside the actual error message (as in the tests we write).</p>
<p>I think most of them are self-explanatory. The *STFail(...)* macro can be called to create an error message without any expression to be checked in the parameter list.</p>
<p>The next test so not very different to the first.</p>
<p>    - (void)testUnsuccessfulCanceling<br />
    {<br />
        [fraction setNumerator:9];<br />
        [fraction setDenominator:5];<br />
        [fraction cancelDown];<br />
        STAssertTrue([fraction numerator] == 9,<br />
          @"Numerator != 9 (is %d)", [fraction numerator]);<br />
        STAssertTrue([fraction denominator] == 5,<br />
          @"Denominator != 5 (is %d)", [fraction denominator]);<br />
    }</p>
<p>I think you got the principle.</p>
<p>    - (void)testCancelingWithZeroNumerator<br />
    {<br />
        [fraction setNumerator:0];<br />
        [fraction setDenominator:5];<br />
        [fraction cancelDown];<br />
        STAssertTrue([fraction numerator] == 0,<br />
          @"Numerator != 0 (is %d)", [fraction numerator]);</p>
<p>        STAssertTrue([fraction denominator] == 5,<br />
          @"Denominator != 5 (is %d)", [fraction denominator]);<br />
    }</p>
<p>    - (void)testGreatestCommonDivisor<br />
    {<br />
        int gcd = [Fraction greatestCommonDivisorOf:119 and:14];<br />
        STAssertTrue(gcd == 7, @"GCD != 7 (is %d)", gcd);<br />
    }</p>
<p>    - (void)testGreatestCommonDivisorWithZeroNumerator<br />
    {<br />
        int gcd = [Fraction greatestCommonDivisorOf:0 and:11];<br />
        STAssertTrue(gcd == 1, @"GCD != 1 (is %d)", gcd);<br />
    }</p>
<p>The sixth test is different as we use the *STAssertThrows(...)* macro which catches thrown exceptions and fails if no exception is thrown.</p>
<p>    - (void)testSetDenominatorThrowingException<br />
    {<br />
        STAssertThrows([fraction setDenominator:0],<br />
           @"setDenominator should throw an exception");<br />
    }</p>
<p>As you should have noticed, the setUp:, tearDown: and all of the test methods return void and don't have any parameters.<br />
Don't forget to put the test method prototypes in your header file (FractionTests.h).</p>
<p>    - (void)testSuccessfulCanceling;<br />
    - (void)testUnsuccessfulCanceling;<br />
    - (void)testCancelingWithZeroNumerator;<br />
    - (void)testGreatestCommonDivisor;<br />
    - (void)testGreatestCommonDivisorWithZeroNumerator;<br />
    - (void)testSetDenominatorThrowingException;</p>
<p>That's it. Let's jump over to implement our Fraction class.</p>
<p>#### Implementing the Fraction class ####</p>
<p>To implement our Fraction class we have to add another class to our project. Click *New File...* in the *File* menu and select *Objective-C class*.</p>
<p><img src="http://www.stiefels.net/wp-content/uploads/2007/05/objc-class.png" height="249" width="300" border="0" align="middle" hspace="4" vspace="4" alt="adding objc class" title="adding objc class" /></p>
<p>Name the class *Fraction* and ensure the class is added to the *UTFraction* target by setting the checkbox.</p>
<p><img src="http://www.stiefels.net/wp-content/uploads/2007/05/objc-class-name-2.png" height="249" width="300" border="0" hspace="4" vspace="4" alt="objc class name" title="objc class name" /></p>
<p>Our header (Fraction.h) file will look something like this:</p>
<p>    #import <cocoa/Cocoa.h></p>
<p>    @interface Fraction : NSObject {<br />
        int _numerator;<br />
        int _denominator;<br />
    }</p>
<p>    + (int)greatestCommonDivisorOf:(int)a and:(int)b;</p>
<p>    - (id)initWithNumerator:(int)numerator denominator:(int)denominator;<br />
    - (void)setNumerator:(int)numerator;<br />
    - (void)setDenominator:(int)denominator;<br />
    - (int)numerator;<br />
    - (int)denominator;<br />
    - (void)cancelDown;</p>
<p>    @end</p>
<p>I won't discuss the implementation of the Fraction class in detail since it is not the topic of this article but I think it should be easy to understand what happens here.</p>
<p>    #import "Fraction.h"</p>
<p>    @implementation Fraction</p>
<p>    + (int)greatestCommonDivisorOf:(int)a and:(int)b<br />
    {<br />
        if (a == 0 || b == 0) return 1;<br />
        if (a < b) return [Fraction greatestCommonDivisorOf:a and:(b-a)];<br />
        if (a > b) return [Fraction greatestCommonDivisorOf:(a-b) and:b];</p>
<p>        return a;<br />
    }</p>
<p>    - (id)init {<br />
        [self initWithNumerator:0 denominator:1];<br />
        return self;<br />
    }</p>
<p>    - (id)initWithNumerator:(int)numerator denominator:(int)denominator<br />
    {<br />
        self = [super init];<br />
        if (self != nil) {<br />
            [self setNumerator:numerator];<br />
            [self setDenominator:denominator];<br />
        }<br />
        return self;<br />
    }</p>
<p>    - (void)setNumerator:(int)numerator<br />
    {<br />
        _numerator = numerator;<br />
    }</p>
<p>    - (void)setDenominator:(int)denominator<br />
    {<br />
        if (denominator == 0) {<br />
            NSException *divisionByZeroException =<br />
              [NSException exceptionWithName:@"DivisionByZeroException"<br />
                                      reason:@"Denominator cannot be zero!"<br />
                                    userInfo:nil];<br />
            @throw divisionByZeroException;<br />
        }<br />
        _denominator = denominator;<br />
    }</p>
<p>    - (int)numerator<br />
    {<br />
        return _numerator;<br />
    }</p>
<p>    - (int)denominator<br />
    {<br />
        return _denominator;<br />
    }</p>
<p>    - (void)cancelDown<br />
    {<br />
        int gcd = [Fraction greatestCommonDivisorOf:[self numerator]<br />
                                                and:[self denominator]];<br />
        [self setNumerator:[self numerator]/gcd];<br />
        [self setDenominator:[self denominator]/gcd];<br />
    }</p>
<p>    @end</p>
<p>As we are going to use Objective-C exceptions we have to turn on the corresponding compiler option.<br />
Right click on the *UTFraction* target, select *Get Info*, switch to the *Build* tab and enable the Objective-C exception flag.</p>
<p><img src="http://www.stiefels.net/wp-content/uploads/2007/05/target-get-info.png" height="172" width="200" border="0" align="middle" hspace="4" vspace="4" alt="get target info" title="get target info" /></p>
<p><img src="http://www.stiefels.net/wp-content/uploads/2007/05/enable-objc-exceptions.png" height="56" width="261" border="0" align="middle" hspace="4" vspace="4" alt="enable objc exceptions" title="enable objc exceptions" /></p>
<p>Also, it is recommended to deactivate *ZeroLink*.</p>
<p><img src="http://www.stiefels.net/wp-content/uploads/2007/05/disable-zerolink.png" height="47" width="251" border="0" align="middle" hspace="4" vspace="4" alt="disable zerolink" title="disable zerolink" /></p>
<p>So, now we have our tests and our class, but how can we run the tests?</p>
<p>#### Dependent and independent mode ####<br />
There are two different modes you can choose from: Independent or dependent.</p>
<p>In independent mode you have to run your tests manually every time you want to see if your code is validated by your tests.<br />
The disadvantage is that this approach can lead to infrequent use of your tests and errors may not be recognized immediately.</p>
<p>In dependent mode the *UTFraction* target is made dependent to the *Unit Tests* target. Every time you compile your code all tests are run automatically.<br />
The advantage is that errors are detected by your tests very soon.</p>
<p>In our tutorial we'll use the dependent mode.</p>
<p>#### Setting up the dependent mode ####<br />
Setting up the dependent mode is not a big thing. All you have to do is adding the *UTFraction* target as a direct dependency of the *Unit Tests* target and set some of the build settings.<br />
Open the Target Info window by right clicking on the *Unit Tests* target and select *Get Info*.</p>
<p><img src="http://www.stiefels.net/wp-content/uploads/2007/05/target-get-info-1.png" height="138" width="247" border="0" hspace="4" vspace="4" alt="get target info" title="get target info" /></p>
<p>Switch to the *General* tab and add the target *Unit Tests* by clicking on the *+* icon at the bottom of the window.</p>
<p><img src="http://www.stiefels.net/wp-content/uploads/2007/05/direct-dependencies-2.png" height="139" width="210" border="0" hspace="4" vspace="4" alt="direct dependencies" title="direct dependencies" /></p>
<p>Switch to the *Build* tab and set the *Bundle Loader* value to *$(BUILT_PRODUCTS_DIR)/UTFraction*.</p>
<p><img src="http://www.stiefels.net/wp-content/uploads/2007/05/bundle-loader.png" height="43" width="300" border="0" hspace="4" vspace="4" alt="bundle loader" title="bundle loader" /></p>
<p>Setting this values tells the linker to use the executable as a additional "framework" so that our class can be used in our tests.</p>
<p>As the *Test Host* setting needs to be set to exact same value you can use *$(BUNDLE_LOADER)* in this field.</p>
<p><img src="http://www.stiefels.net/wp-content/uploads/2007/05/test-host.png" height="36" width="293" border="0" hspace="4" vspace="4" alt="test host" title="test host" /></p>
<p>Setting the *Test Host* value tells the Unit Test framework to "inject" your tests into the executable.</p>
<p>If you are building an application you have to set the value of the *Bundle Loader* and *Test Host* setting to the path of your executable (e.g. *$(BUILT_PRODUCTS_DIR)/MyApp.app/Contents/MacOS/MyApp*).<br />
If you are developing a framework or a shared library the *Test Host* value should not be set.</p>
<p>Close the window.<br />
Be sure to select the *Unit Tests* target as your *Active Target*.</p>
<p><img src="http://www.stiefels.net/wp-content/uploads/2007/05/active-target.png" height="63" width="214" border="0" align="middle" hspace="4" vspace="4" alt="active target" title="active target" /></p>
<p>Now, every time you start compiling your code the tests are run automatically after the compilation process ended successfully.<br />
If you don't want that the tests are run, simply select the *UTFraction* target as your *Active Target*.</p>
<p>#### Evaluating the tests ####<br />
Whether you use dependent or independent mode the tests are run as a shell script. If any test fails you'll get an error in Xcode. In this example all test should run without errors.</p>
<p>To see how it would look like if tests fail, we change the implementation of the greatestCommonDivisor: method by setting the first line to:</p>
<p>    if (a == 0 || b == 0) return 2;</p>
<p>Compile your code and you'll get two errors. Two tests failed!</p>
<p><img src="http://www.stiefels.net/wp-content/uploads/2007/05/errors.png" height="71" width="300" border="0" align="middle" hspace="4" vspace="4" alt="errors" title="errors" /></p>
<p>Change the line back to the original one, compile it and you'll see no more errors.</p>
<p>#### The End ####<br />
Well, that's it. This tutorial should have given you a small insight in the unit testing framework included in Xcode.</p>
<p>You can download the full tutorial project [here](http://www.stiefels.net/wp-content/uploads/2007/05/UTFraction.zip).</p>
<p>If you are interested in further information, have a look at [Apple's documentation about Unit Tests](http://developer.apple.com/documentation/DeveloperTools/Conceptual/UnitTesting/index.html).</p>
<p>If you have any suggenstions or comments, please don't hesitate to leave a line (or more) in the comments.</p>
<p>Thank you and happy testing!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stiefels.net/2007/05/01/unit-testing-with-xcode/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
