Simplifying the objc binding

Tagged:  •  

The previous post I have show you an example on how to create a simple windows using the objective-c binding.

Still it was working it was not so flexible. If you fetch the latest commit from github you will have enhancement quoted below:

  1. Direct access to ObjC class
  2. Unary method pragma for method override
  3. Super send
  4. Transparent String and Array
  5. Bug fix

Note the the binding can run on OSX and use gnustep for other platform. See my previous post for installation details

1. Direct access to ObjC class

Thanks to gnu-smalltalk namespace, Objective-c class can now be access directly from Objc.

newWindow = Objc.NSWindow alloc.

2. Unary method pragma for method override

When subclassing a objective-c class it was require that you add the type encoding of the method. Now if you know that the method you are adding is in the superclass the binding will find the type encoding for you.

For example if you override the -drawRect: method of NSView you can simply do:

Objc.ObjcObject subclass: MyNSView [
    <objcSubclass: 'NSView'>

    drawRect: rect [
        <objc>
        Objc.NSColor redColor set.
        Objc.ObjcAppKit nsRectFill: self bounds
    ]
]

Note that for this feature you will need to fetch the latest gst from my repository or wait for the patch. This is because unary pragma are currently not supported in gst.

3. Super send

In a method that you override you can now do a super send. This is particularly useful when overriding -init

init [
    <objc>
    super init
]

Super send to objective-c object will only forward the lookup to the ObjC runtime if the class where the super send is written is a direct subclass of Objc.ObjcObject

4. Transparent String and Array

String and Array can now be directly pass as argument in message send to objective-c object.

nsWindow setTitle: 'Test window'

The string is just wrap around a subclass of NSString by the binding so you don't need to send the #asNSString message

That is it for the time being. Fell free to comment or ask me questions.

Many thanks to David Chisnall who helps me a lot to get the type encoding right.

User login