Re: Encoding an integer in another object
Re: Encoding an integer in another object
- Subject: Re: Encoding an integer in another object
- From: David Remahl <email@hidden>
- Date: Wed, 04 Sep 2002 17:49:56 +0200
>
I would like my Document to encode an int from my WindowController.
>
Since the second parameter to encodeValueOfObjCType:at: is an address, I
>
can not use an ordinary access routine that returns the value, not the
>
address. So I tried this kludge that seem to work:
>
>
In the Document:
>
[coder encodeValueOfObjCType:@encode(unsigned int) at:[wController
>
addressOfMyInt]];
>
>
In the WindowController:
>
- (void *)addressOfMyInt {
>
return &myInt;
>
}
>
>
Is there a more elegant solution to this?
An alternative:
int myInt = [wController myInt]; // call the int accessor on the wController
[coder encodeValueOfObjCType:@encode(unsigned int) at:&myInt];
>
According to the declaration of encodeValueOfObjCType:at: the second
>
param should be (const void*)addr, but if I use this type in my
>
accessor, the compiler protests on the decoding version which has (void
>
*)data.
>
>
- (void)encodeValueOfObjCType:(const char *)type at:(const void *)addr;
>
- (void)decodeValueOfObjCType:(const char *)type at:(void *)data;
>
>
I would be very grateful if anyone could explain to me why the
>
parameters are declared differently. In Learning Cocoa they are used in
>
the same way which makes sense to me if I assume that they are both addresses.
Because the decode method changes the memory pointed at by the data
argument, while the encode method only reads it. The const qualifier
indicates that the called function/method will not change the referenced
data (enforced at compile time, not at run time).
When you decode the value, do it like this:
int myInt;
[coder decodeValueOfObjCType:@encode(unsigned int) at:&myInt];
[wController setMyInt:myInt];
/ Med vdnliga hdlsningar, David Remahl
>
Thanks,
>
Bertil
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.