• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Casting Object to NSArray
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Casting Object to NSArray


  • Subject: Re: Casting Object to NSArray
  • From: Chuck Hill <email@hidden>
  • Date: Sat, 28 Jun 2003 18:11:04 -0700

Hi,

Me.  Again.  :-)

At 08:06 PM 28/06/2003 -0400, Goodbye Bill wrote:
>Karl / Art,
>
>I DID do as Art requested.  The object returned "java.lang.Object" and not
>NSArray.  I could not successfully cast it to an NSArray and/or use it.
>That is the whole problem.
>
Did it say java.lang.Object or did it say [Ljava.lang.Object?  Contrary to
what a person (me!) might initially  assume the '[L' is not junk printed
out by a crappy piece of buggy code.  It has, in fact, a definite if
obscure meaning.  For clues on how to decode this less than intuitive
information see:

http://java.sun.com/j2se/1.3/docs/api/java/lang/Class.html#getName()

In a nutshell, java.lang.Object is Object and [Ljava.lang.Object is
Object[].  Yes Virginia, that garbage is supposed to have meaning.  ;-)


>If it was an "Object[]" I would assume that I should be able to simply pull
>out individual objects from THAT array.  Is that not correct?
>
That is correct, using ((Object[])thatObject)[0] etc.

>Even better, I would like to be able to change it back to an NSArray.
>
A reasonable desire and one met by the frameworks:
NSArray thatObjectAsNSArray = new NSArray(thatObject);

>Is it possible to cast an "Object[]" to an "NSArray?"  If so, how?
>
No, they can not be cast back and forth.  An NSArray can be constructed
from Object[] and an NSArray can convert itself into an Object[] by asking
it for its objects().


>Also, should I be able to use the "Object[]" directly?
>
Certainly, but if the method states that it returns Object you will need to
cast it first:
(Object[])returnedObject

Welcome to the paranoid nightmare of casting in Java.

Chuck


>On 6/28/03 4:03 AM, "Hsu" <email@hidden> wrote:
>
>> Art is correct. There are several classes that have serializers but no
>> deserializers - NSArray is one of them. In goes an NSArray, out comes
>> an Object[].
>>
>> Karl
>>
>> On Friday, June 27, 2003, at 08:18  PM, Art Isbell wrote:
>>
>>> On Friday, June 27, 2003, at 04:35  PM, Goodbye Bill wrote:
>>>
>>>> Thanks for all of the replies, however I left out one small detail
>>>> that will
>>>> probably change all of your answers: this is via a web service.
>>>>
>>>> The code on the SERVER (the WebService) looks like this...
>>>>
>>>>     private NSArray myObjects;
>>>>
>>>>     public NSArray GetObjects()
>>>>     {
>>>>         return myObjects;
>>>>     }
>>>>
>>>> The code on the CLIENT (the consumer app) looks like this...
>>>>
>>>>     public NSArray myReceivedObjects()
>>>>     {
>>>>         Object tmpArray = serviceClient().invoke(serviceName(),
>>>>                           "GetObjects", null);
>>>>         return (NSArray)tmpArray;
>>>>     }
>>>>
>>>> Here's a little more info on this:
>>>>
>>>> 1. The "serviceClient" is a "WOWebServiceClient" object;
>>>> 2. According to the API Reference, the "invoke" method on this object
>>>> returns an "Object" regardless of what method it invokes on the
>>>> server.
>>>>
>>>> The client example above will compile but generates a
>>>> "ClassCastException"
>>>> exception at runtime.
>>>>
>>>> Any ideas?  I'm going on two full days working with this.  The bottom
>>>> line
>>>> is that I need to get an array of objects from a web service.
>>>
>>> The documentation for invoke() states, "This method may return either
>>> an object, or a language array of objects."  I read "language array of
>>> objects" to mean an Object[] (i.e., a primitive Java array) rather
>>> than an NSArray.  Apparently you didn't do what I suggested: print
>>> tmpArray.getClass().getClassName() to see what class of object is
>>> returned.
>>>
>>> Aloha,
>>> Art
>>> _______________________________________________
>>> webobjects-dev mailing list | email@hidden
>>> Help/Unsubscribe/Archives:
>>> http://www.lists.apple.com/mailman/listinfo/webobjects-dev
>>> Do not post admin requests to the list. They will be ignored.
>>>
>>>
>> --
>>
>> If I want to hear the pitter patter of little feet, I'll put shoes on
>> my cat.
>>
>> Homepage:
>>     http://homepage.mac.com/khsu/index.html
>> _______________________________________________
>> webobjects-dev mailing list | email@hidden
>> Help/Unsubscribe/Archives:
>> http://www.lists.apple.com/mailman/listinfo/webobjects-dev
>> Do not post admin requests to the list. They will be ignored.
>_______________________________________________
>webobjects-dev mailing list | email@hidden
>Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/webobjects-dev
>Do not post admin requests to the list. They will be ignored.
>

--

Chuck Hill                                 email@hidden
Global Village Consulting Inc.             http://www.global-village.net
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Re: Casting Object to NSArray (From: Hsu <email@hidden>)
 >Re: Casting Object to NSArray (From: Goodbye Bill <email@hidden>)

  • Prev by Date: Re: Casting Object to NSArray
  • Next by Date: Re: Casting Object to NSArray
  • Previous by thread: Re: Casting Object to NSArray
  • Next by thread: Re: Casting Object to NSArray
  • Index(es):
    • Date
    • Thread