Re: Dynamic Instance Variables, is id the answer?
Re: Dynamic Instance Variables, is id the answer?
- Subject: Re: Dynamic Instance Variables, is id the answer?
- From: Uli Kusterer <email@hidden>
- Date: Sun, 8 Oct 2006 12:35:36 +0200
Am 08.10.2006 um 02:09 schrieb John Stiles:
And if not, the answer is "void*".
Actually, void* is probably /not/ the answer. Here's three better
suggestions:
1) Use id with a protocol (i.e. id<MyValueProtocol>) and write some
subclasses (or categories) on the kinds of objects you want to store
that implement that protocol. You can use NSNumber and NSValue to
wrap ints and other C data types. That way, you can treat all of
these data the same way, and only the code creating them has to know
how they are different.
2) Use a union, plus an enum indicating the type. Then write
accessors that check the type before treating the data as that type.
But really, that's more a lower-level implementation of #1, and less
safe, so I'd recommend against it.
3) If you have no clue what type the data is you may be getting, use
NSData. NSData is Cocoa's equivalent to malloc(), if I may say that
in such a simplified way. You'll usually need to allocate storage for
it anyway, even if you used a void*. Instead of using malloc, use
NSData and stash it it an ivar, that way you can retain/release it etc.
If you told us a little more about the /high level/ task you're
trying to achieve, we might be able to make a better solution. Are
you trying to implement a variant data type that transparently
converts between types for a scripting language? Are you creating a
database engine that can store ints and blobs? What is this for?
There are few apps who actually want to store *arbitrary* kinds of
data, because if you don't know what type the data is, you can't work
on it.
Cheers,
-- M. Uli Kusterer
http://www.zathras.de
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden