On Fri, Nov 5, 2010 at 9:36 AM, Dan Hopwood <d...@biasdevelopment.com> 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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to