4
Sep
2008
 

Cocoa Tutorial: Creating your very own framework

by Marcus Zarra

There are numerous situations where creating your own framework is advantageous. Perhaps you have a block of code that you use repeatedly in many different projects. Perhaps you are building a plug-in system for your application and want the infrastructure to be available both to the application and to any plugins that are coded.

A Cocoa framework is another project type in Xcode. The end result is a bundle, similar to an application bundle. Inside of this bundle is the compiled code you wrote and any headers that you want exposed. The headers are important. Without them, just like any other piece of Objective-C code, it is very hard to code against.

(more…)

 
27
Aug
2008
 

Cocoa Tutorial: Sync Services without Core Data

by Marcus Zarra

Sync Services have come a long way in Leopard. Before Leopard it was an extremely complex operation that was almost completely manual. Needless to say, this sucked and it was probably one of the reasons it was shunned by most developers.

If you are using Core Data in a Leopard application then Sync Services is so trivial that you should be syncing if it makes sense. In this article we are going to cover syncing in a non-Core Data situation as that is quite a bit more complex.

If you have read the Sync Services documentation then you know it is complex. Let me dispel an illusion right away. It is hard. It is not poor documentation, syncing is very hard and very few people get it right. Take a look at Omnifocus to see an example of a company thinking it is easy and losing data. Therefore if you are expecting this subject to be trivial you will be disappointed.

In this example we will be syncing with the bookmarks schema and displaying them in a simple outline view. The outline view itself will be editable and those edits can be synced back. Not terribly useful but provides a very simple example.
(more…)

 
18
Aug
2008
 

Cocoa Tutorial: libxml and xmlreader

by Marcus Zarra

Let us pretend for a moment that NSXMLDocument was not available to your Cocoa application for some reason. Perhaps you have low memory requirements, perhaps you are running on a slimmed down version of OS X. Whatever the reason, for the purposes of this exercise, NSXMLDocument does not exist.

Let us now assume that we have a requirement to parse an xml document quickly and without loading the entire tree into memory in a object structure. In a situation like this libxml comes in handy. Unfortunately it is quite a bit more complicated than calling alloc init on NSXMLDocument.

libxml is a C library that is included with all current releases of OS X. With this library we can quickly read in a document, scrape the information we need out of that document and avoid loading the entire tree into memory at once. In addition, libxml (and more specifically xmlReader) does this very quickly, far faster than NSXMLDocument which is very useful when you have a lower end CPU. In this project we are going to create a simple application that reads in an xml file containing a list of people, their names and their ages. For the purposes of demonstration we are going to load that data into an array of NSDictionary objects and display it in a standard Cocoa window.

(more…)

 
9
Jul
2008
 

A git quickie

by Marcus Zarra

After reading Fraser Speirs’ excellent write-up on his conversion over to git, I followed a few of the links to find a bash script to display your current git branch in the command prompt. Following yet another link from that post showed how to convert the bash script to zsh.

Here is my contribution to move that from the prompt to the right side of the screen.

function parse_git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
function precmd() { RPS1="%~$(parse_git_branch)" }
export PS1='> '

Note that the last line just gives me a very short left hand prompt which I prefer.

GitBranchDisplay
Uploaded with plasq‘s Skitch!

 
8
Jul
2008
 

A Case Against Dot Syntax

by Marcus Zarra

I was not born an Objective-C developer. I know that in some circles that is considered a mortal sin. Before learning Objective-C and Cocoa I had developed in a great number of languages dating back to the early 1980’s. I tell you this so that what you are about to read next is taken in the light that it was intended.

Take a look at the following lines of code:

- (void)doSomethingSpecial
{
	myVar.itsAttribute = 10;
	myOtherVar.itsAttribute = 20;
}

Now tell me, what is myVar and myOtherVar. Is it an object or a struct? Can’t tell from that piece of code can you. That is half of my argument against dot syntax in Objective-C. It makes the meaning of your code unclear. Objective-C is known for its self documenting nature. Dot Syntax removes that.

(more…)

 
8
Jul
2008
 

The audacity of [some] Windows Developers

by Marcus Zarra

Thanks to our beloved iPhone (I do refrain from calling it “My Precious”), we have seen a sudden influx of Windows Developers. Now, when most of us came over to OS X and Objective-C from whatever platform we hailed from we did not assume that everything would be the same. Most of us are reasonable people and realize that OS X is different for a reason. Unfortunately, it appears that we are unusual people. Perhaps this would explain why we came over to this platform before it became “popular”.

With this recent influx of developers, most of whom we have welcomed with open arms, there are some who expect everything to be the same as the platform they came from and without bothering to learn or experiment have proclaimed our development tools to be “prehistoric”. This truly amazes me.

First, welcome to OS X and iPhone development. This is not the same language, platform and API you have been dealing with. Accept that or go home. We are not going to change it to suit you. We like it just the way it is.

We do things differently over here. Accept that or go home.

You have an interest in either OS X or the iPhone. To do a proper application for either one (barring a few edges cases), you need to learn Objective-C and Cocoa. Accept that … well you get the idea.

Objective-C has been around for a long time and it is a well thought out language. It is a runtime focused language and therefore things work differently than you are used to in your more structured environments.

Most of the time when these so called developers complain about Objective-C I simply roll my eyes and walk the other way. It is the sane thing to do. Never wrestle with a pig — you get dirty and the pig likes it. However, one particular “genius” has decided to out himself on his own blog. Of course I speak of none other than Jesse Ezell.

It is clear from this blog post that he has no interest in learning why OS X, Cocoa and Objective-C are different from his beloved Visual Studio but instead cries that it is too hard. I mean, seriously, complaining about NSObject vs Object? Perhaps he did not bother to learn that there is more than one root object in Objective-C? And then go to on and complain about MVC like its the devil’s music? Hopefully he is not the best that .net has to offer us!

But even with all of that, I read his post, chucked and moved on. It was not until he responded to the comments on that post that I decided to respond. It seems, from his perspective, that if a developer cares enough about their development environment to respond to his rant (and try to educate him!) that we are all “rabit elitists” out to get him!

First the word is rabid, not rabit. If you were using OS X you could have seen that it was misspelled and used the dictionary to figure out what the word meant. If you can’t even bother to run a spellchecker why bother writing at all?

Second, we care about our platform. We care about the code that we produce and how our applications look and are presented to the user. I know that is probably an extremely foreign concept where he comes from. But we care!

When developers come over here with preconceptions they do everyone a disservice. If they cannot even be bothered to pick up a book and read about the language to understand its fundamentals and its tools then why bother complaining about it. They are a waste of space. Move over and let someone who is willing to learn step up to the plate.

As for this developer’s ego and contempt for the developers on this platform — shame on him. His arrogance speaks towards his ignorance. He probably has written more lines of code in the past few years than I have. I have found that applications on Windows tends to take ten times as many lines of code as the same application would written on Objective-C and Cocoa. That does not make this developer better — if anything it makes him worse.

My suggestion is this: Pick up a book and read. You can even just read blogs like this one and avoid having to pay any money to learn. If a developer can’t be bothered then go home, we have no interest in you and certainly have no need for you.

 
29
May
2008
 

Coding Practice: Cleaning up the default Core Data project

by Marcus Zarra

In this entry I am going to do something a little different. Instead of showing some wicked code, I am going to reformat one of Apple’s default templates.

Apple writes some amazing code. Unfortunately a lot of their templates demonstrate simply terrible coding practices. What is worse, people take these templates and assume that they are the proper practice for coding in Objective-C and propagate that poor code into their own projects.

The first example I am going to tackle is the AppDelegate class that is auto generated in the Core Data template. This is not the Core Data Document template but the standard application template.

(more…)

 
5
May
2008
 

WWDC 2008 T-Shirts!

by Marcus Zarra

May 20, 2008
The T-Shirts for delivery at WWDC have been ordered. If there is enough interest I will setup a way to purchase CIMGF T-shirts (non-WWDC) directly from the site. Please contact me at marcus at cimgf dot com if this is of interest to you.


Last year I wore t-shirts for Zarra Studios and got quite a few compliments. These days I am becoming more known for this blog than anything else. Therefore, I am doing different t-shirts this year to celebrate this blog. As a reader of this blog you have an opportunity to purchase one of these shirts. They will only be run for WWDC so they are limited edition! :)

While the design is not complete yet, here is the first public draft of the T-Shirts:

Public_Draft_1.png

Normal (Male) T-Shirt

WWDC2008F_Proof_1.png

V-Neck (Female) T-Shirt

Currently, the plan is to take them with me to WWDC and hand them out to those who have purchased one right after the keynote on Monday. If you are not going to WWDC but would still like to order a T-Shirt, that is also doable but naturally shipping costs will be involved.

The price per shirt will be $20.00 US. If you are interested in purchasing one or more, please contact me at marcus at cimgf dot com and I will work out the details. Right now I am processing orders manually but if they get too crazy then I may set up an actual shopping cart. Quantities are limited so act now! ;-)

Lastly, to follow Justin’s great idea. I am interested in trading T-shirts with other WWDC attendees. If you have a NEW shirt that you would like to trade for a CIMGF shirt, please contact me at marcus at cimgf dot com to plan a swap.

All Orders must be received and paid for by the 19th of May, 2008. Any orders received after that date can be shipped but cannot be delivered to WWDC.

Legal Junk

It is not my intention to make a profit on these t-shirts. I am offering them to readers at the approximate production cost. It is not my intent to mass produce these t-shirts with the goal of making a profit. If the production run comes close to or exceeds 500,000 t-shirts (wouldn’t that be amazing!) then the artwork on the back of the t-shirts may change due to restrictions in the license agreement.

 
23
Apr
2008
 

Cocoa Tutorial: Don’t Be Lazy With NSDecimalNumber (Like Me)

by Marcus Zarra

NSDecimalNumber is Objective-C’s solution to numbers that need to be very precise. The documentation defines it as:

NSDecimalNumber, an immutable subclass of NSNumber, provides an object-oriented wrapper for doing base-10 arithmetic. An instance can represent any number that can be expressed as mantissa x 10^exponent where mantissa is a decimal integer up to 38 digits long, and exponent is an integer from –128 through 127.

NSDecimalNumber

If you are dealing with currency at all, then you should be using NSDecimalNumber. However, since it is immutable and definitely not a primitive then it is difficult to use right? Well — yes — a bit. But if you do not want to see your $9.50 item displayed as $9.49999994 or something then you are better off using NSDecimalNumber right from the beginning. Otherwise you are going to be converting to it later and that is a LOT more painful.

(more…)

 
15
Apr
2008
 

Cocoa Tutorial: Get The Most Out of Key Value Coding and Observing

by Marcus Zarra

Key Value Observing/Key Value Coding (KVO/KVC) is probably one of the most powerful and most under-utilized features of Objective-C. Here are a couple of examples of how to get the most out of it

When a call is made on an object through Key Value Coding such as [self valueForKey:@"someKey"], numerous attempts are made to resolve the call. First, the method someKey is looked for. If that is not found then the iVar someKey is looked for. If neither of those are found, then one last attempt is made before presenting an error. That last attempt is a call to the method -(id)valueForUndefinedKey:. If that method is not implemented then an NSUndefinedKeyException is raised.

valueForUndefinedKey: is designed so that when you request a value from an object using -(id)valueForKey: the object has a last chance to respond to that request before an error occurs. This has many benefits and I have included two examples of those benefits in this post–Core Data Parameters and Data Formatting.

(more…)

 
13
Apr
2008
 

Git and XCode: A git build number script

by Marcus Zarra

Git has been gaining in popularity with Cocoa developers as well as open source developers. As I work it into my development workflow, one item that was missing was the automatic injection of the build number into the application bundle.

There are a few scripts floating around that perform this trick for subversion, but git handles build numbers a bit differently and it appears that no one has bothered to publish one. As is known, subversion uses an incrementing integer for build numbers. This makes it very easy to determine which build number came first and makes it very useful for a non-public version number. Git, however, uses a hash for each build number which is not incrementing and therefore not very useful for determining version numbers. However, it is still very useful for pulling up a specific build when a crash report is received, etc.

Therefore, with the help of Matt Long’s perl-fu, I have updated Daniel Jalkut’s subversion perl script to work with git. Since the build numbers are not sequential, I would not recommend using them for Sparkle. Therefore, in my own build process for iWeb Buddy, I hand select the version number (for example 1.0.4) and then use the short hash from git as the CFBundleVersion number. Normally this number is displayed in parens after the primary build number but, at least in iWeb Buddy, I have removed it from the display entirely. Since it is no longer a sequential number it would only potentially confuse users and it displays in the crash reports anyway.

The updated script is as follows:

# Xcode auto-versioning script for Subversion by Axel Andersson
# Updated for git by Marcus S. Zarra and Matt Long

use strict;

# Get the current git commit hash and use it to set the CFBundleVersion value
my $REV = `/opt/local/bin/git show --abbrev-commit | grep "^commit"`;
my $INFO = "$ENV{BUILT_PRODUCTS_DIR}/$ENV{WRAPPER_NAME}/Contents/Info.plist";
    
my $version = $REV;
if( $version =~ /^commit\s+([^.]+)\.\.\.$/ )
{ 
	$version = $1;
}
else
{
	$version = undef;
}
die "$0: No Git revision found" unless $version;
    
open(FH, "$INFO") or die "$0: $INFO: $!";
my $info = join("", );
close(FH);
    
$info =~ s/([\t ]+CFBundleVersion<\/key>\n[\t ]+).*?(<\/string>)/$1$version$2/;
    
open(FH, ">$INFO") or die "$0: $INFO: $!";
print FH $info;
close(FH);

Since git is distributed, there is no need to be online to produce a build. The script will grab the current abbrev-commit hash and will inject it into the current build’s Info.plist file.

 
4
Apr
2008
 

Cocoa Tutorial: Using NSError to Great Effect

by Marcus Zarra

Error handling is rarely fun. I find myself often re-coding a method after I realize that I need to handle one error condition or another. Usually, error handling involves either try/catch or some return code strategy. Neither of those is pretty or easy to maintain. For Objective-C development, however, there is another option — NSError.
(more…)