I was recently asked for an Objective-C to Nu conversion guide. That’s a good idea, and although it’s not yet written, I have a few example conversions online that may be helpful. One big one is NuPagePacker, my Nu port of Aaron Hillegass’ open-source PagePacker program. I’ve also posted the git repository for NuAnimatingViews, a port of the AnimatingViews example in the Leopard Developer Tools. That example uses CoreAnimation and required some improvements to Nu that aren’t in any existing release. To run it, you’ll need to either build your Nu from my git repository or wait for the next release of Nu.
After looking back over my code and the glimpses I’ve gotten of other people’s Nu code, here are some thoughts on the substance and style of programming in Nu:
Pointers to instances of Objective-C classes should always be declared of type
For enumerated types, use
Arguments that pass Objective-C class instances by reference should be declared of type
(id *). On the Nu side, they are instances of the NuReference class.
Methods declared as
(IBAction) in Objective-C should be declared to return
(void) in Nu.
IBOutlets are just instance variables. Declare them in Nu with the
ivar operator. Their type should be
@ in front of your Nu strings.
Don’t call retain and release; Nu automatically retains objects when they are assigned to values and releases when new values are assigned or the associated contexts are released.
Use the new
import macro (in the git repository only) to read function and constant definitions from the Leopard BridgeSupport files.
(import Cocoa) loads all of the definitions for Cocoa.
Watch that your class
+initialize methods are getting called. For pure-Nu class implementations, you will need to call them yourself. Put the call in your application delegate’s
Make sure that your methods have the correct type signature. If you don’t specify a signature (by leaving off the type specifiers), Nu will first try to look up the method in the runtime. If it finds it, the signature of that method will be used. If not, the return value and all arguments are assumed to be of type
To check the signature of a method, examine:
((MyClass instanceMethodWithName:"methodWithArg1:arg2:") signature)
Also, currently type signatures are all-or-nothing. If you declare a method with partial type information, the method declaration will fail. So either specify types for a method’s return value and all its arguments, or omit them all and use the default.
Refer to selectors by name: use
"myMethod:" instead of
Code formatting is a menial task, and menial tasks are for machines (and Pythonistas). Use nubile to format your code. The easiest way to do that is to use TextMate. If you can’t use nubile, use emacs.
Don’t leave parentheses dangling on separate lines. Unless someone beats me to it, someday I’m going to enhance nubile to automatically pull up dangling closing parentheses.
Symbols that end with a colon (’:’) are called labels. The Nu parser recognizes the colon as the end of a symbol, so you don’t need a space between a label and the value that follows it. Leave it out; that makes the association more clear. I’m considering also enforcing this with nubile.
Use the other Nu tools: nuke, nutest, and nudoc. I’ve learned a lot about my code from using each of them.
I’ll come back and add more to this list as I think of it. If you have suggestions or questions, email me or post a comment below.