Exploring Cocoa

You will have many many questions about how Cocoa works.  Often times you want an answer even quicker than having to create a little program for it.  Apple’s API is massive and you often just want to see the result of a particularly simple message.

F-Script to the rescue! F-Script is a handy tool by Phillipe Mougin which allows you to interact with Apple’s APIs.

You can grab the beta of F-Script 2.0 here.

Unzip it and drop it into your Applications directory.  Launch F-Script.

You will be greeted with something that looks awfully similar to Squeak’s System Browser.  That’s because F-Script is a Smalltalk like scripting language!

So what can you do with it? For example perhaps you want to know something about how NSDate works.

Launch XCode and open up the Documentation window which is listed under the Help menu.  Make sure that you are doing an API search and not a full-text search.  Search for NSDate.

Select the class documentation from the Core Library (not iPhone or iPhone 2.2).  Apple’s documentation program is basically a fancy HTML viewer.  On the right side you’ll see some disclosure triangles that can be expanded. Expand Class Methods and Instance Methods.  If you don’t know what class methods are you need to reread the Objective-C Programming Guide.

+ (id)dateWithNaturalLanguageString:(NSString *)string

In the Class Methods list you should see something like the above.  Wow there’s a method to convert a natural language string into a date.  Want to try it out?  When F-Script opened it also opened up a F-Script workspace, similar to the Workspace in Squeak but it doesn’t act like a regular text editor.  Type the following at the prompt and press enter.

NSDate alloc init

You just allocated and initialized a NSDate object, look Ma! No XCode! Notice that you don’t need brackets. This is how Smalltalk does it, but note that it’s pretty much conceptually the same as Objective-C. Now let’s try out that fancy method. Like Smalltalk, F-Script strings are indicated with single quotes not double quotes.

NSDate dateWithNaturalLanguageString:'yesterday 5pm'

You should get a date object that represents yesterday at 5 o’ clock in the afternoon. Pretty amazing. If there’s ever a method or class that you want to try out in your project, just open up F-Script.

fscript

Tips

  1. You can create variables on the fly, just assign values to them with :=
  2. You don’t need brackets.  Messages are interpreted left to right.
  3. You get basic code completion by typing a few characters and hitting the ESC key.
  4. Strings are represented with single quotes, not double quotes.
  5. {} is shorthand for defining arrays if you need them.
  6. Check out the F-Script site for more info.