Archive for October, 2006

NSPredicate: The integrated query language

Tuesday, October 24th, 2006

Often it’s hard to keep up with the all the new cool stuff. Java 6 is around the corner and only one of my projects already uses Java 5.

The same is for Cocoa. Apple is finishing Leopard while I’m still learning APIs and enhancements of Tiger. So was it today when I remembered something from the WWDC that’s called NSPredicate.

In order to find out if an application with a specific bundle identifier (unique key) is running you would do something like this:

int index;
NSArray* apps = [[NSWorkspace sharedWorkspace] launchedApplications];
for(index=0; index < [apps count]; index++) {
    NSString* identifier = [[apps objectAtIndex:index] 
                    objectForKey: @"NSApplicationBundleIdentifier"];
    if ([identifier isEqualToString: someIdentifier]) {
        // do something and leave for loop            
    }
}

Tiger introduced NSPredicate which is used by core data to define queries. But it can also be used to search for items in collections. So doing it with NSPredicate is as simple as this:

[NSPredicate predicateWithFormat:@"NSApplicationBundleIdentifier=%@",
                                 someIdentifier]];
NSArray* apps = [[[NSWorkspace sharedWorkspace] launchedApplications] 
                          filteredArrayUsingPredicate:predicate];
if ([apps count]==1) {
     // do something
}

The real benefit comes of course when your query is more complex. So beside using while or for to filter a collection just take a look at NSPredicate. It can make things a bit easier. Note: Tiger only :-)

First Responder: A Dynamic Heaven

Sunday, October 22nd, 2006

I grew up with programming languages like Pascal, C, C++ and later Java. All of these languages have a rather static type system. The compiler of these languages can do a lot of good things. It checks for example if you try to use functions that are not valid in a particular context.

These kind of static programming is good for a number of cases and perhaps it’s the best environment for many mission critical systems. At least every bug that is being found by the compiler must not be found by QA.

But this life belt comes at a cost.

(more…)

Cocoa Directory Services Wrapper

Sunday, October 15th, 2006

Dirservices Sample

Mac OS X uses the Directory Service for user authentication and more. The service can be configured to communicate with an Active Directory installation or a LDAP installation to get user information.

There is also a framework to get access to the functionality of this service. So far so good. Unfortunately this framework is a very very very low level framework and for whatever reason it has some very strange naming conventions for functions and data structures.

Searchlight authenticates a user through a Ruby on Rails application to give the user the correct Spotlight results. So I needed the help of Directory Services which cost me a lot of time.

That’s one of the problems when you have to deal with a not so hype framework. It does not have as much documentation, sample code, tutorials like those cool image, animation, … stuff.

(more…)

Cocoa, Carbon and the Human Factor

Tuesday, October 10th, 2006

Lately there was some discussion, initiated by Wil Shipley about Carbon (C API) and Cocoa (Objective-C API) and which one provides a better abstraction.

I love Cocoa and I love to have as much APIs as possible available in Cocoa. But one fact that was forgotten in the discussion is the influence of the framework developers.

I code - But am I a framework developer?

The language a programmer chooses to implement an API/Framework has an effect on the elegance and interface of an API. But his personal experience in the topic and in framework development in general has a much greater effect. (more…)