Dynamic Instance Variables, is id the answer?

  • I need my class's instance variables to be dynamic in the sense that
    they could be pointers to any kind of data, for example, pointers to
    ints, NSObjects, structs, etc.

    Is id the answer?  I hear it's the "generic type", but haven't seen
    good examples of it's use.  If so, I'm not sure how to use id (proper,
    declaration, initialization, pointing, referencing, etc.).

    If not id, then how do I accomplish this dynamic ability for instance
    variables to be pointers to any type of data?

    @interface DynamoClass : NSObject
    {
            id pointerOne;
            id pointerTwo;
            id pointerThree;
    }

    - (id)initWithString:(NSString *)dataType;

    @end

    Thanks for any pointers. ;
  • On Oct 7, 2006, at 4:12 PM, Jordan Evans wrote:

    > I need my class's instance variables to be dynamic in the sense that
    > they could be pointers to any kind of data, for example, pointers to
    > ints, NSObjects, structs, etc.
    >
    > Is id the answer?  I hear it's the "generic type", but haven't seen
    > good examples of it's use.  If so, I'm not sure how to use id (proper,
    > declaration, initialization, pointing, referencing, etc.).
    >
    > If not id, then how do I accomplish this dynamic ability for instance
    > variables to be pointers to any type of data?
    >
    > @interface DynamoClass : NSObject
    > {
    > id pointerOne;
    > id pointerTwo;
    > id pointerThree;
    > }
    >
    > - (id)initWithString:(NSString *)dataType;
    >
    > @end
    >
    > Thanks for any pointers. ;

    "id" is a pointer to an Objective-C object of any kind. If you want a
    truly generic pointer then void* is your answer.

    With that said I am not sure I grasp what you really are trying to
    achieve (it sounds problematic).

    -Shawn
  • http://developer.apple.com/documentation/Cocoa/Reference/
    ObjCRuntimeRef/Reference/reference.html

    id is the answer if all your ivars are NSObjects. Just create
    NSObject wrappers for your other types.

    On Oct 7, 2006, at 7:12 PM, Jordan Evans wrote:

    > I need my class's instance variables to be dynamic in the sense that
    > they could be pointers to any kind of data, for example, pointers to
    > ints, NSObjects, structs, etc.
    >
    > Is id the answer?  I hear it's the "generic type", but haven't seen
    > good examples of it's use.  If so, I'm not sure how to use id (proper,
    > declaration, initialization, pointing, referencing, etc.).
    >
    > If not id, then how do I accomplish this dynamic ability for instance
    > variables to be pointers to any type of data?
    >
    > @interface DynamoClass : NSObject
    > {
    > id pointerOne;
    > id pointerTwo;
    > id pointerThree;
    > }
    >
    > - (id)initWithString:(NSString *)dataType;
    >
    > @end
    >
    > Thanks for any pointers. ;
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/robmartin%
    > 40frontiernet.net
    >
    > This email sent to <robmartin...>
    >
  • And if not, the answer is "void*".

    Robert Martin wrote:
    > http://developer.apple.com/documentation/Cocoa/Reference/ObjCRuntimeRef/Ref
    erence/reference.html

    >
    >
    > id is the answer if all your ivars are NSObjects. Just create NSObject
    > wrappers for your other types.
    >
    >
    >
    > On Oct 7, 2006, at 7:12 PM, Jordan Evans wrote:
    >
    >> I need my class's instance variables to be dynamic in the sense that
    >> they could be pointers to any kind of data, for example, pointers to
    >> ints, NSObjects, structs, etc.
    >>
    >> Is id the answer?  I hear it's the "generic type", but haven't seen
    >> good examples of it's use.  If so, I'm not sure how to use id (proper,
    >> declaration, initialization, pointing, referencing, etc.).
    >>
    >> If not id, then how do I accomplish this dynamic ability for instance
    >> variables to be pointers to any type of data?
    >>
    >> @interface DynamoClass : NSObject
    >> {
    >> id pointerOne;
    >> id pointerTwo;
    >> id pointerThree;
    >> }
    >>
    >> - (id)initWithString:(NSString *)dataType;
    >>
    >> @end
    >>
    >> Thanks for any pointers. ;
    >> _______________________________________________
    >> Do not post admin requests to the list. They will be ignored.
    >> Cocoa-dev mailing list      (<Cocoa-dev...>)
    >> Help/Unsubscribe/Update your Subscription:
    >> http://lists.apple.com/mailman/options/cocoa-dev/<robmartin...>
    t

    >>
    >>
    >> This email sent to <robmartin...>
    >>
    >
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/<jstiles...>
    >
    > This email sent to <jstiles...>
  • Hi,

    I'd vote for void* if you know the real type assigned (e.g. char, int
    or n objectiveC type)
    If you need to find the type at runtime stick with ids and
    obbjectiveC classes.

    Am Oct 8, 2006 um 1:22 AM schrieb Robert Martin:

    > http://developer.apple.com/documentation/Cocoa/Reference/
    > ObjCRuntimeRef/Reference/reference.html
    >
    > id is the answer if all your ivars are NSObjects. Just create
    > NSObject wrappers for your other types.
    >
    >
    >
    > On Oct 7, 2006, at 7:12 PM, Jordan Evans wrote:
    >
    >> I need my class's instance variables to be dynamic in the sense that
    >> they could be pointers to any kind of data, for example, pointers to
    >> ints, NSObjects, structs, etc.
    >>
    >> Is id the answer?  I hear it's the "generic type", but haven't seen
    >> good examples of it's use.  If so, I'm not sure how to use id
    >> (proper,
    >> declaration, initialization, pointing, referencing, etc.).
    >>
    >> If not id, then how do I accomplish this dynamic ability for instance
    >> variables to be pointers to any type of data?
    >>
    >> @interface DynamoClass : NSObject
    >> {
    >> id pointerOne;
    >> id pointerTwo;
    >> id pointerThree;
    >> }
    >>
    >> - (id)initWithString:(NSString *)dataType;
    >>
    >> @end
    >>
    >> Thanks for any pointers. ;
    >> _______________________________________________
    >> Do not post admin requests to the list. They will be ignored.
    >> Cocoa-dev mailing list      (<Cocoa-dev...>)
    >> Help/Unsubscribe/Update your Subscription:
    >> http://lists.apple.com/mailman/options/cocoa-dev/robmartin%
    >> 40frontiernet.net
    >>
    >> This email sent to <robmartin...>
    >>
    >
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/<dominik...>
    >
    > This email sent to <dominik...>
    >
  • 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
  • Thanks for the advice everyone.  Lots to think about.  I'm choosing id
    and primitive wrappers first to get a draft out of this concept class.

    On 10/8/06, Uli Kusterer <witness.of.teachtext...> wrote:

    > 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.

    I'm creating an all-purpose bridging object.  For example, a bridge
    for any Controller class to a view class.  Right now I need to cache
    some images, but only show parts of them, i.e. scrolling.  But I've
    ran into this problem at several different times and have wrote
    bridges that are conceptually identical, just have different types and
    ranges, i.e., writing 2 gig+ files.
previous month october 2006 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today