atom feed5 messages in net.java.dev.rococoa.usersRe: Class hierarchies, NSArray, and c...
FromSent OnAttachments
Matt ReynoldsSep 22, 2008 3:12 pm 
Duncan McGregorSep 22, 2008 3:15 pm 
Matt ReynoldsSep 22, 2008 3:24 pm 
Duncan McGregorSep 24, 2008 3:03 pm 
Matt ReynoldsSep 24, 2008 3:12 pm 
Subject:Re: Class hierarchies, NSArray, and casting
From:Duncan McGregor (dun@oneeyedmen.com)
Date:Sep 24, 2008 3:03:55 pm
List:net.java.dev.rococoa.users

We don't seem to have a wiki. Downcasting is covered as the last section of the whistlestop tour

https://rococoa.dev.java.net/rococoa-whistlestop.html

I guess proving that wherever we put it, it can be missed ;-)

On Mon, Sep 22, 2008 at 11:24 PM, Matt Reynolds <ma@hom3.us> wrote:

Yay! Sorry, I should have found that on my own. This works great!

Is there a way to add these sorts of examples into the wiki or some other place? I think they might be useful to people getting into Rococoa.

On Mon, Sep 22, 2008 at 3:16 PM, Duncan McGregor <dun@oneeyedmen.com> wrote:

It looks like Rococoa.cast(NSObject object, Class<T> desiredType) should do it for you.

On 22 Sep 2008, at 23:12, Matt Reynolds wrote:

I'm running this : public class AddressBookTest { public static QTKit instance = (QTKit) Native.loadLibrary("AddressBook", QTKit.class);

/** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { System.out.println("Starting, instance created : " + instance); NSAutoreleasePool pool = NSAutoreleasePool.new_(); System.out.println("Java version : " + System.getProperty("java.version"));

_Class addressBookClass = ABAddressBook.CLASS; ABAddressBook addressBook = addressBookClass.sharedAddressBook(); NSArray people = addressBook.people(); System.out.println("People count : " + people.count()); for(int i = 0; i < people.count() ; i++) { NSObject objectAtIndex = people.objectAtIndex(i); System.out.println("Obj : " + objectAtIndex.getClass()); ABPerson person = (ABPerson) objectAtIndex; System.out.println("Person : " + person); }

pool.release(); System.out.println("Done"); } }

Currently, this fails because I can't cast NSObject to ABPerson because they're, well, not the same or in the same hierarchy. So, any tips on how I'd go about doing the above? I checked out JNA and didn't find a trick handy. Is this obvious and I'm missing it?