Colour Me Cocoa http://www.colourmecocoa.com Exploring the world of cocoa development for Mac OS X. Thu, 19 Jul 2007 08:12:13 +0000 http://wordpress.org/?v=2.2.2 en Melbourne CocoaHeads: First Meeting (updated) http://feeds.feedburner.com/~r/ColourMeCocoa/~3/134866294/ http://www.colourmecocoa.com/2007/07/18/melbourne-cocoaheads-first-meeting/#comments Wed, 18 Jul 2007 09:50:37 +0000 Gareth Townsend http://www.colourmecocoa.com/2007/07/18/melbourne-cocoaheads-first-meeting/
moved next door to the bunker. come downstairs

As of a few weeks ago, Melbourne got itself a CocoaHeads group. It’s all the fault of Daniel Jalkut and a chance meeting at the 2007 WWDC developer bash.

Tomorrow night, July 19th, at 6pm, we’re holding our first meeting at The Order Of Melbourne on Swanston Street, opposite RMIT University.

If you’re in town then come join us, look for the Apple logos.

]]>
http://www.colourmecocoa.com/2007/07/18/melbourne-cocoaheads-first-meeting/feed/ http://api.feedburner.com/awareness/1.0/GetItemData?uri=ColourMeCocoa&itemurl=http%3A%2F%2Fwww.colourmecocoa.com%2F2007%2F07%2F18%2Fmelbourne-cocoaheads-first-meeting%2Fhttp://www.colourmecocoa.com/2007/07/18/melbourne-cocoaheads-first-meeting/
Apple Treats Student Developers As 3rd Class Citizens (updated) http://feeds.feedburner.com/~r/ColourMeCocoa/~3/123767259/ http://www.colourmecocoa.com/2007/06/10/apple-treats-student-developers-as-3rd-class-citizens/#comments Sun, 10 Jun 2007 16:23:28 +0000 Gareth Townsend http://www.colourmecocoa.com/2007/06/10/apple-treats-student-developers-as-3rd-class-citizens/ Since arriving in San Francisco on Friday I have been pumped for the Steve Jobs Keynote on Monday morning. I’ve spent a bit of time planning my Monday morning, afterall the keynote is the main event of the week.

Today when picking up my WWDC badge and goodie bag I was informed that all students will be declined entry to the keynote and re-directed to an over flow room. I flew half way around the world and will now be forced to once again watch the keynote on a TV screen.

NOT! HAPPY! APPLE!

update

Upon entering Moscone West yesterday all of the students were sent off to wait in a room downstairs. We weren’t allowed out of that room until about 10.10am. When we were finally allowed out we were directed to the same over-flow rooms as everyone else, only the keynote had already started. I came in just as the Harry Potter demo was finishing.

]]>
http://www.colourmecocoa.com/2007/06/10/apple-treats-student-developers-as-3rd-class-citizens/feed/ http://api.feedburner.com/awareness/1.0/GetItemData?uri=ColourMeCocoa&itemurl=http%3A%2F%2Fwww.colourmecocoa.com%2F2007%2F06%2F10%2Fapple-treats-student-developers-as-3rd-class-citizens%2Fhttp://www.colourmecocoa.com/2007/06/10/apple-treats-student-developers-as-3rd-class-citizens/
Uncle Jens’ Coding Tips http://feeds.feedburner.com/~r/ColourMeCocoa/~3/114705103/ http://www.colourmecocoa.com/2007/05/06/uncle-jenss-coding-tips/#comments Mon, 07 May 2007 05:54:05 +0000 Gareth Townsend http://www.colourmecocoa.com/2007/05/06/uncle-jenss-coding-tips/ Jens Alfke has posted some great coding tips for working with large projects. Small projects become large projects become coding nightmares. So do yourself a favour and form good coding habits early.

]]>
http://www.colourmecocoa.com/2007/05/06/uncle-jenss-coding-tips/feed/ http://api.feedburner.com/awareness/1.0/GetItemData?uri=ColourMeCocoa&itemurl=http%3A%2F%2Fwww.colourmecocoa.com%2F2007%2F05%2F06%2Funcle-jenss-coding-tips%2Fhttp://www.colourmecocoa.com/2007/05/06/uncle-jenss-coding-tips/
What Is Core Data? http://feeds.feedburner.com/~r/ColourMeCocoa/~3/114492395/ http://www.colourmecocoa.com/2007/05/05/what-is-core-data/#comments Sun, 06 May 2007 04:19:48 +0000 Gareth Townsend http://www.colourmecocoa.com/2007/05/05/what-is-core-data/ Apple’s developer tools are based around a design pattern known as the Model-View-Controller (MVC). The simplest definition of MVC is to keep your data and the rules for using it (the model) separate from your view (the user interface) and to then tie them together using your Controller. The idea is that in doing so it becomes possible to re-use parts of your code on other projects.

When we built our first cocoa application our view was built in Interface Builder (using pre-built user interface components), while our controller code was written in Xcode using cocoa bindings. Our first cocoa application did not have a model.

Core Data is Apple’s framework for developing your applications data store. Lets take a look at what it can do for you and your applications.

Building A Data Store

Nearly every application I have ever worked on has required some form of data store. This is usually done by serializing your data objects into a text file using your format of choice.

Reading and writing data to and from text files is a lot of work. You have to translate your data objects into text, you also have to translate text back into data. On top of this you need to make sure all the data is deconstructed and re-assembled in the correct order and that the text file itself has not been corrupted by a nosey user editing it directly.

Core Data makes working with data simple. Open up Xcode and create a new project. Select “Core Data Application” from the new project assistant (it’s under the Application heading). I’ve called mine coreDataTest.

Core Data Project

Open up the coreDataText_DataModel.xcdatamodel file by double clicking on it. Xcode will open up a window you probably haven’t seen before. This is the window in which you can edit core data models.

Empty Core Data Model

In the top left box you can add an entity, the box to the right of it is where you can edit your new entities properties. The panel on the far right lets you set all of the options related to an entity while the grid paper area below shows a visual representation of your data model.

There are four types of properties you can give to an entity, but we are going to focus on the following two today:

  • Attributes
  • Relationships

Attributes are the values that make up your entity. A person for example might have a firstName, a surname and an age field. This is exactly like building a table in a database and naming the columns. Use the plus symbol to add a person entity with the above properties.

A relationship is a connection between two entities. A person has a parent, we can represent this in our core data model by adding a relationship called parent with a Destination of Person. Go ahead and add a relationship in the same way you added attributes. You should end up with something similar to the following:

Core Data Person Model

That was easy wasn’t it? Our model here is extremely simple, but it should provide you with enough to get started.

Creating A User Interface For A Core Data Model Quickly

Creating a user interface from a core data model is equally simple. Open up Interface Builder. Option-Click the visual representation of your model and drag it onto a new window in Interface Builder. Elect to have a User Interface for many person objects created. And you’re done!

Person Model In Interface Builder

Save your Interface Builder files and use Xcode to build and run your application. You didn’t write any code, yet you have a fully functioning application that that remembers its state and has built in undo/redo functionality. That’s really powerful!

That being said, you probably wouldn’t want to release your Interface Builder generated user interface as part of your next killer application. But it’s useful when trying to see how your model works and if there is something missing.

Want To Learn More?

No doubt you will want to learn more about Core Data. The following two links from the ADC should get you started. If anyone knows of any other Core Data tutorials that are worth reading then let us all know in the comments and I’ll add them to the list.

Updated: 8th May 2007
Reason: Re-wrote the MVC definitions to be more concise and accurate.

]]>
http://www.colourmecocoa.com/2007/05/05/what-is-core-data/feed/ http://api.feedburner.com/awareness/1.0/GetItemData?uri=ColourMeCocoa&itemurl=http%3A%2F%2Fwww.colourmecocoa.com%2F2007%2F05%2F05%2Fwhat-is-core-data%2Fhttp://www.colourmecocoa.com/2007/05/05/what-is-core-data/
WWDC Countdown http://feeds.feedburner.com/~r/ColourMeCocoa/~3/109713063/ http://www.colourmecocoa.com/2007/04/17/wwdc-countdown/#comments Tue, 17 Apr 2007 10:03:36 +0000 Gareth Townsend http://www.colourmecocoa.com/2007/04/17/wwdc-countdown/ Everyone reading this should know about WWDC by now. If you don’t then I have no idea what rock you have been living under but you should probably come out and bask in the sun.

Today is an important day for me. The Apple Universities Consortium here in Australia just announced the winners of this years WWDC student scholarships. 4 people from RMIT will be attending this year, and I am one of them.

To celebrate I’ve downloaded the countdown widget, to remind myself that I’ll be sitting in the Moscone center in just over 54 days. Hopefully I’ll see some of you there!

WWDC 2007 Countdown Widget
Get the WWDC 2007 countdown widget.

]]>
http://www.colourmecocoa.com/2007/04/17/wwdc-countdown/feed/ http://api.feedburner.com/awareness/1.0/GetItemData?uri=ColourMeCocoa&itemurl=http%3A%2F%2Fwww.colourmecocoa.com%2F2007%2F04%2F17%2Fwwdc-countdown%2Fhttp://www.colourmecocoa.com/2007/04/17/wwdc-countdown/
I Don’t Work For __MyCompanyName__ http://feeds.feedburner.com/~r/ColourMeCocoa/~3/107058521/ http://www.colourmecocoa.com/2007/04/06/i-dont-work-for-__mycompanyname__/#comments Fri, 06 Apr 2007 09:33:27 +0000 Gareth Townsend http://www.colourmecocoa.com/2007/04/06/i-dont-work-for-__mycompanyname__/ Doesn’t it just annoy you that every time you create a new file in Xcode you have to change the company name? I don’t work for __MyCompanyName__, do you? Today I got sick of replacing that block of text and went in search of a solution.

The answer can be found in the Xcode 2.4 Expert Preferences Notes. PBXCustomTemplateMacroDefinitions is a dictionary that allows you to setup macros for use in the default templates. ORGANIZATIONNAME is the macro that defaults to __MyCompanyName__ and we can set that using the terminal.

Open up the terminal (found in Applications/Utilities/) and run the following command to set ORGANIZATIONNAME to whatever is appropriate for you. I’ve used my name. Please note, you should run this all on one line, I’ve only added line breaks to make it fit nicely on the page.

defaults write com.apple.xcode
PBXCustomTemplateMacroDefinitions
'{ ORGANIZATIONNAME = "Gareth Townsend"; }'

Now whenever Xcode creates a new file my name will be placed in the automatic copyright comments. Hooray!

There are a lot of other expert preferences listing in the documentation, so have a look through it and see if there’s something else of interest to you. If you do find something interesting then be sure to let us know in the comments.

]]>
http://www.colourmecocoa.com/2007/04/06/i-dont-work-for-__mycompanyname__/feed/ http://api.feedburner.com/awareness/1.0/GetItemData?uri=ColourMeCocoa&itemurl=http%3A%2F%2Fwww.colourmecocoa.com%2F2007%2F04%2F06%2Fi-dont-work-for-__mycompanyname__%2Fhttp://www.colourmecocoa.com/2007/04/06/i-dont-work-for-__mycompanyname__/
Did You Know About The Ternary Operator? http://feeds.feedburner.com/~r/ColourMeCocoa/~3/105710442/ http://www.colourmecocoa.com/2007/03/31/did-you-know-about-the-ternary-operator/#comments Sun, 01 Apr 2007 02:07:52 +0000 Gareth Townsend http://www.colourmecocoa.com/2007/03/31/did-you-know-about-the-ternary-operator/ I just found out about this operator while working through Brian Christensen’s excellent Write a Screen Saver tutorial.

radius = rect.size.width <= rect.size.height ?
           rect.size.width / 2 : rect.size.height / 2;

Weird looking isn’t it? Here’s the simplified version:

(CONDITION) ?  EXPRESSION1 : EXPRESSION2;

How Does It Work?

If the CONDITION statement evaluates to true then EXPRESSION1 is run, if the CONDITION statement evaluates to false then EXPRESSION2 is run. You can think of it as a compressed if-else statement.

How Has Brian Used It?

Brian has used the question mark operator to assign a value to radius based on the evaluation of rect.size.width <= rect.size.height.

radius = CONDITION ?
           EXPRESSION1 : EXPRESSION2;

We could re-write Brian’s code with an if-else statement.

if (rect.size.width <= rect.size.height)
{
	radius = rect.size.width / 2;
}
else
{
	radius = rect.size.height / 2;
}

It does the same thing, but uses more lines of code. Whether it is easier to read or not is a matter of personal opinion, but knowing about its existence can only be a good thing.

You never know when you will have to maintain someone else’s code.

]]>
http://www.colourmecocoa.com/2007/03/31/did-you-know-about-the-ternary-operator/feed/ http://api.feedburner.com/awareness/1.0/GetItemData?uri=ColourMeCocoa&itemurl=http%3A%2F%2Fwww.colourmecocoa.com%2F2007%2F03%2F31%2Fdid-you-know-about-the-ternary-operator%2Fhttp://www.colourmecocoa.com/2007/03/31/did-you-know-about-the-ternary-operator/
Building Your First Cocoa Application http://feeds.feedburner.com/~r/ColourMeCocoa/~3/105341198/ http://www.colourmecocoa.com/2007/03/30/building-your-first-cocoa-application/#comments Fri, 30 Mar 2007 10:19:04 +0000 Gareth Townsend http://www.colourmecocoa.com/2007/03/30/building-your-first-cocoa-application/ Today I’m going to take you on a journey through building your first cocoa application. We’re going to start by defining a simple problem, then build a user interface and through the wonders of cocoa bindings connect it all up to a controller object.

I am going to use the challenge from Chapter 4 of Cocoa Programming For Mac OS X by Aaron Hillegass as our problem. The solution requires the user to be able to enter a string into a text field, press a button, and have the application display another string indicating the length of the original string.

For example if I type in “Colour Me Cocoa rules!” and click the button the application will respond with

“Colour Me Cocoa Rules!” is 22 characters long

Sounds simple enough… So if you have just under an hour or so to spare then follow me!

Create a New Cocoa Application

Open Xcode and create a new project. Select Cocoa Application from under the Application header. I’ve called mine Challenge4, but you can call yours whatever you like.

You should have a list of files in front of you, locate the one called MainMenu.nib (English). This is your user interface file. Double click it and wait. Interface Builder will open and you will be presented with the default interface builder windows. You might find it useful to use the Hide Others command from the Interface Builder menu as you will have quite a few windows on screen.

Interface Builder with an empty MainMenu.nib file open

Building the User Interface

Because we are going to use cocoa bindings we won’t be needing a button on our interface, but we will require 2 NSTextField objects. Using the palletes window (found under tools) choose the cocoa-text menu and drag System Font Text and the NSTextField box below it onto the window entitled “Window”.

Cocoa Text

Re-size and organise your 2 NSTextField objects and the window itself. You will notice little blue dotted lines appear when you bring these objects close to the edge of the window. Use these guides to help lay out and align your objects. When you have finished you should end up with something similar to this:

Window

This looks great so far, but the “System Font Text” string and the title of the window look wrong. Lets fix them. From the Tools menu select Inspector. Depending on what you have selected in your main window, the inspector window will give you different options to edit. Change the “Window Title:” to something different, I’m using “String Length Calculator”, and change the “System Font Text” to be “Enter a string above”.

Window 2

Creating a Controller Class

Now that you have built a user interface it’s time to create a controller class. This class will be responsible for ensuring the program knows what to do with the string you enter into the text field.

In interface builder find the window entitled “MainMenu.nib (English)” and choose the Classes heading. Scroll to the left and select NSObject. Every class within Cocoa ultimately descends from NSObject.

Subclassing Nsobject

While NSObject is selected, choose Subclass NSObject from the Classes menu item. This will create a new Object called “MyObject”. Rename this to “LengthCalculator”.

Now that you have created a class, it’s time to create the files that go along with it. With LengthCalculator selected choose Create Files for LengthCalculator from the Classes menu. Make sure all the check boxes are ticked and that you are then hit the Choose button.

Creating Length Calculator Files

Save your interface builder files and return to Xcode. You should now have LengthCalculator.h and LengthCalculator.m files in your project.

Writing The Controller Code

First we need to flesh out our header file. Open LengthCalculator.h and modify it as follows:

	/* LengthCalculator */

	#import <Cocoa/Cocoa.h>

	@interface LengthCalculator : NSObject
	{
		NSString * inputString;
	}

	-(NSString *)outputString;

	@end

The above code defines how LengthCalculator works. Lets take a closer look at it.

@interface is the signifies the beginning of our definition. Likewise @end signifies the end of our definition. Everything in the header should be contained within these two keywords.

LengthCalculator : NSObject declares that LengthCalculator is a sub-class of NSObject, or that NSObject is LengthCalculator’s super-class. In Objective-C each class can only have one super-class.

NSString * inputString; contained within the curly braces defines an NSString object called inputString. This will soon be connected to our NSTextField box in the UI.

-(NSString *)outputString; is a method that we will have to implement in LengthCalculator.m. It returns an NSString object and we will connect that to the other NSTextField object on our user interface. This will allow us to display our answer.

Now for the meaty bits. Open LengthCalculator.m and modify it to contain the following:

#import "LengthCalculator.h"

@implementation LengthCalculator

+ (void)initialize
{
	[LengthCalculator setKeys:
		[NSArray arrayWithObjects:@"inputString", nil]
		triggerChangeNotificationsForDependentKey:@"outputString"];
}

- (NSString *)outputString
{
	if ([inputString length] > 0) {
		return [NSString stringWithFormat:
		@"\"%@\" is %d characters long", inputString, [inputString length]];
	}
	return [NSString stringWithFormat:@"Enter a string above"];
}

@end

This is the implementation of the LengthCalculator controller class. You probably guessed that from the @implementation keywords right?

Because we are going to use cocoa bindings to wire up our user interface we need to implement the + (void)initialize method. This method is called just before the class receives its first message—even if that message would not create an object.. The code within the initialize method might look a bit scary, but it’s really very simple.

In this code we are telling the LengthCalculator object to create key bindings, we are doing this with the setKeys method. The key bindings that we have created are defined in the NSArray that is created. Specifically we have added inputString as a key.

triggerChangeNotificationsForDependantKey is a mouthful of a method call. But it does just what it seems. It takes the keys in the array (ie. inputstring) and then notifies the dependent key outputString if it changes.

Re-read the above sentence again because it is important. This is how we set up our cocoa bindings. Now whenever inputString changes, the outputString method will be called.

- (NSString *)outputString
{
	if ([inputString length] > 0) {
		return [NSString stringWithFormat:
		@"\"%@\" is %d characters long",inputString, [inputString length]];
	}
	return [NSString stringWithFormat:@"Enter a string above"];
}

If you have been reading the other articles on this site then it will not take you too long to figure out how the outputString method works. I’m not going to explain it here, if you’re stuck then go back and read about controlling the flow of your program and format specifiers in the FizzBuzz solution.

Connecting the User Interface to the Controller

Nearly there. Just one thing left to do. We need to connect everything together so that it works. Open up MainMenu.nib again. Find and highlight LengthCalculator under the classes heading in the MainMenu.nib window. Now select Instantiate LengthCalculator from the Classes menu.

Instantiate Length Controller

Look at the shiny blue box you just created. Isn’t it pretty? This is a representation of a LengthCalculator object. Back in the Palletes window there is a similar blue box in the menu along the top. Click it and you will now be looking at the Cocoa-Controllers pallete.

Cocoa Controllers

Drag the middle green box, an NSObjectController, next to your blue box in the MainMenu.nib window. You can re-name it if you want, but I am going to leave it as is.

Now that we have instances of both LengthCalculator and NSObjectController, we need to tell the NSObjectController that it is in charge of the LengthCalculator. We do this by control clicking on the NSObjectController and dragging to the LengthCalculator.

The inspector will pop up with content highlighted. Click the connect button to seal the deal.

With the inspector still open change the drop-down from Connections to Attributes and use the Add button to create some keys. You guessed it, inputString and outputString.

Nsobject Controller Keys

Now select the top NSTextField, back in the inspector change the drop down to Bindings. Hit the value button to expose the details. Select inputString from the Model Key Path drop down. You have just told the NSTextField to bind itself to the inputString key-binding.

Bindings

Note that I have also ticked the Continually Updates Values checkbox. Without this checked the user has to press enter or click outside the NSTextField before it will signal that it has been changed. With it checked every key press will signal a change.

Do the same for the other NSTextField, only this time bind it to outputString.

The Moment Of Truth

So the time has finally come. Save your files and hit the Build and Run in Xcode. If you’ve follow all the steps then your first cocoa program will launch and work as intended. Pat yourself on the back.

If something goes wrong, try and work it out, go back and read through the instructions again. Try starting from scratch, sometimes stupid little mistakes cause the biggest problems and they are an absolute nightmare to find. Over time you will learn what these mistakes are, but for now it will probably be quicker to start from scratch.

Remember, if you’re still having problems then leave a comment. We’ll try and set you on the right path again.

More Tutorials

I find the best way to learn is by doing. And the more I do the more I learn. These little applications don’t take very long to make so set yourself a problem and get cracking on your own.

I’m not going to leave you hanging though. Here are a few other tutorials I’ve used and recommend:

]]>
http://www.colourmecocoa.com/2007/03/30/building-your-first-cocoa-application/feed/ http://api.feedburner.com/awareness/1.0/GetItemData?uri=ColourMeCocoa&itemurl=http%3A%2F%2Fwww.colourmecocoa.com%2F2007%2F03%2F30%2Fbuilding-your-first-cocoa-application%2Fhttp://www.colourmecocoa.com/2007/03/30/building-your-first-cocoa-application/
Apple Expands ADC on iTunes http://feeds.feedburner.com/~r/ColourMeCocoa/~3/105013264/ http://www.colourmecocoa.com/2007/03/28/apple-expands-adc-in-itunes/#comments Wed, 28 Mar 2007 21:40:45 +0000 Gareth Townsend http://www.colourmecocoa.com/2007/03/28/apple-expands-adc-in-itunes/ Apple have just updated the ADC section of iTunes with a whole lot more developer love. log in and treat yourself to a total of 16 Leopard Sessions From WWDC 06 along with 18 Scientific Poster Session Podcasts.

If you’re not an ADC member then sign up now, the free online membership is all that is required to access the iTunes section.

]]>
http://www.colourmecocoa.com/2007/03/28/apple-expands-adc-in-itunes/feed/ http://api.feedburner.com/awareness/1.0/GetItemData?uri=ColourMeCocoa&itemurl=http%3A%2F%2Fwww.colourmecocoa.com%2F2007%2F03%2F28%2Fapple-expands-adc-in-itunes%2Fhttp://www.colourmecocoa.com/2007/03/28/apple-expands-adc-in-itunes/
Iron Coder V http://feeds.feedburner.com/~r/ColourMeCocoa/~3/104809796/ http://www.colourmecocoa.com/2007/03/27/iron-coder-v/#comments Wed, 28 Mar 2007 00:20:08 +0000 Gareth Townsend http://www.colourmecocoa.com/2007/03/27/iron-coder-v/ Iron Coder V has been announced and will be running from March 30th to April 1st.

What is Iron Coder?

Straight from the horse’s mouth…

The #macsb Iron Coder contest gives Mac programmers an API, a theme, and 48 hours to make something cool.

That sounds very cool. If you’re not a complete beginner you might want to consider putting aside a weekend to work on something different. Even if you don’t create a working solution to the problem you will definitely learn something more about an API.

If you are a complete beginner you might want to pay attention anyway to see what kind of applications are born from the Iron Coder event. There’s nothing like a set of constraints and a deadline to bring out the creativity monsters.

House Keeping

As an aside, for those of you wondering, the next tutorial should be up by Friday. We’re going to build our first cocoa application, hooray!

]]>
http://www.colourmecocoa.com/2007/03/27/iron-coder-v/feed/ http://api.feedburner.com/awareness/1.0/GetItemData?uri=ColourMeCocoa&itemurl=http%3A%2F%2Fwww.colourmecocoa.com%2F2007%2F03%2F27%2Firon-coder-v%2Fhttp://www.colourmecocoa.com/2007/03/27/iron-coder-v/
http://api.feedburner.com/awareness/1.0/GetFeedData?uri=ColourMeCocoa