• 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: NSDictionary casting
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSDictionary casting


  • Subject: Re: NSDictionary casting
  • From: Sherm Pendley <email@hidden>
  • Date: Fri, 5 Nov 2010 10:43:24 -0400

On Fri, Nov 5, 2010 at 9:36 AM, Dan Hopwood <email@hidden> wrote:
>
> I am using XML-RPC technology to invoke a website back end. I used the
> Wordpress project to extract what I needed in order to send off requests and
> decode what I get back from the webservice. This works.
>
> The encoding/decoding methods equate the *struct* tag to the type *
> NSDictionary* for obvious reasons. In order to form the NSDictionary object
> to encode however requires me to set the name value pairs manually, which is
> annoying. I wondered whether it's possible to instead define a custom class,
> which contains the attributes I want to send and then cast the object to
> type NSDictionary, which the encoder is then able to interpret.
>
> In the same way, if a struct is returned by the webservice I'd like to be
> able to cast the NSDictionary object to a custom class.

If I understand your question correctly, then no, a type cast won't do
what you want. Casting an Objective-C object is a compile-time
construct that tells the compiler to assume that an object is of the
specified type, to avoid warnings about unknown messages. But it does
*not* actually convert the object - that is, it has absolutely no
effect at run time. An object is what it is, regardless of any type
casting.

What you could do instead is extend NSDictionary with a "convenience
constructor" class method. Similarly, you could create an instance
method that would return an instance of your custom class from a
dictionary.

@interface NSDictionary (MyWordpressExtensions)

+(NSDictionary*) dictionaryWithMyCustomObject:(MyCustomObject *)obj;
- (MyCustomObject *) customObject;

@end

True, you'd still need to write the necessary conversion code manually
- but you'd only need to do so once, and it wouldn't clutter up your
code outside of these methods. You'd use a different syntax to do the
conversion, but it would be just as brief and easy to read as your
proposed type cast. Assuming that you have a "wordpress" object that
returns an NSDictionary from -getResponse, the type casting you
propose might look like this:

  // Hypothetical conversion by type cast, WILL NOT WORK!
  MyCustomObject *obj = (MyCustomObject*)[wordpress getResponse];

The alternative, using a category method such as the above to do the
conversion, would look like this:

  MyCustomObject *obj = [[wordpress getResponse] customObject];

sherm--

--
Cocoa programming in Perl:
http://camelbones.sourceforge.net
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: NSDictionary casting
      • From: Fritz Anderson <email@hidden>
References: 
 >NSDictionary casting (From: Dan Hopwood <email@hidden>)

  • Prev by Date: Map peel affect
  • Next by Date: Re: Threads and run loops
  • Previous by thread: NSDictionary casting
  • Next by thread: Re: NSDictionary casting
  • Index(es):
    • Date
    • Thread