float[4][3] into NSValue

  • Can I store a float[4][3] into an NSValue object?
    Or can I store my own struct in an NSValue?
    If yes, how to do that?
    And how to get back the float[4][3] from the NSValue?

    Best Regards
    --
    Lorenzo
    email: <archidea...>
  • I don't believe you can. Try NSData instead. You can store pretty much
    everything in there, but watch out for endianness issues if you store
    floats and/or structs. Check

    http://developer.apple.com/documentation/Cocoa/Conceptual/Archiving/

    Specifically the chapter on Encoding and Decoding C Data Types. It has
    some more pointers on what your options are.

    Good luck,
    Hank

    On Nov 20, 2007, at 2:16 PM, Lorenzo wrote:

    > Can I store a float[4][3] into an NSValue object?
    > Or can I store my own struct in an NSValue?
    > If yes, how to do that?
    > And how to get back the float[4][3] from the NSValue?
    >
    >
    >
    > Best Regards
    > --
    > Lorenzo
    > email: <archidea...>
    >

    Hank Heijink
    <hank.list...>
  • On 20 Nov 2007, at 19:16, Lorenzo wrote:

    > Can I store a float[4][3] into an NSValue object?
    > Or can I store my own struct in an NSValue?
    > If yes, how to do that?
    > And how to get back the float[4][3] from the NSValue?

    No, NSValues are for single values, not arrays of them.

    If you're just looking for a way to encapsulate your array and pass it
    around, try NSData.
  • On Nov 20, 2007, at 11:37 AM, Paul Sargent wrote:

    >
    > On 20 Nov 2007, at 19:16, Lorenzo wrote:
    >
    >> Can I store a float[4][3] into an NSValue object?
    >> Or can I store my own struct in an NSValue?
    >> If yes, how to do that?
    >> And how to get back the float[4][3] from the NSValue?
    >
    > No, NSValues are for single values, not arrays of them.

    Why do you say that? NSValue can store arbitrary structures, as long
    as they are of fixed length.

    float test[4][3]    = {{1.0, 2.0, 3.0}};
    float result[4][3]    = {0.0};

    NSValue *value = [NSValue value:test withObjCType:@encode(float[4][3])];

    [value getValue:result];

    NSLog(@"%f %f %f", result[0][0], result[0][1], result[0][3]);

    >>
    2007-11-20 11:54:03.553 test[18824] 1.000000 2.000000 0.000000

    Allen
  • On 20 Nov 2007, at 19:57, Allen Smith wrote:

    >
    > On Nov 20, 2007, at 11:37 AM, Paul Sargent wrote:
    >
    >>
    >> On 20 Nov 2007, at 19:16, Lorenzo wrote:
    >>
    >>> Can I store a float[4][3] into an NSValue object?
    >>> Or can I store my own struct in an NSValue?
    >>> If yes, how to do that?
    >>> And how to get back the float[4][3] from the NSValue?
    >>
    >> No, NSValues are for single values, not arrays of them.
    >
    > Why do you say that? NSValue can store arbitrary structures, as long
    > as they are of fixed length.
    >
    > float test[4][3]    = {{1.0, 2.0, 3.0}};
    > float result[4][3]    = {0.0};
    >
    > NSValue *value = [NSValue value:test withObjCType:@encode(float[4]
    > [3])];
    >
    > [value getValue:result];
    >
    > NSLog(@"%f %f %f", result[0][0], result[0][1], result[0][3]);
    >
    >
    >>>
    > 2007-11-20 11:54:03.553 test[18824] 1.000000 2.000000 0.000000

    Ok, I didn't know that.

    Consider me slapped.
  • Thank you Allen, it worked like a charm. And this is a great news.
    I can now store a lot of data in a single line. I suppose this is
    endian-safe. It will work on Intel ad PPC. Right?
    I mean, I can write values on Intel and read values on PPC and viceversa,
    right?

    Best Regards
    --
    Lorenzo
    email: <archidea...>
    >
    > From: Allen Smith <Allen.Smith...>
    > Date: Tue, 20 Nov 2007 11:57:56 -0800
    > To: Paul Sargent <psarge...>
    > Cc: Lorenzo <archidea...>, <cocoa-dev...>
    > Subject: Re: float[4][3] into NSValue
    >

    >
    > On Nov 20, 2007, at 11:37 AM, Paul Sargent wrote:
    >
    >>
    >> On 20 Nov 2007, at 19:16, Lorenzo wrote:
    >>
    >>
    >>> Can I store a float[4][3] into an NSValue object?
    >>> Or can I store my own struct in an NSValue?
    >>> If yes, how to do that?
    >>> And how to get back the float[4][3] from the NSValue?
    >>>
    >>
    >> No, NSValues are for single values, not arrays of them.
    >
    > Why do you say that? NSValue can store arbitrary structures, as long as they
    > are of fixed length.
    >
    > float test[4][3] = {{1.0, 2.0, 3.0}};
    > float result[4][3] = {0.0};
    >
    > NSValue *value = [NSValue value:test withObjCType:@encode(float[4][3])];
    >
    > [value getValue:result];
    >
    > NSLog(@"%f %f %f", result[0][0], result[0][1], result[0][3]);
    >
    >
    >>>>>
    > 2007-11-20 11:54:03.553 test[18824] 1.000000 2.000000 0.000000
    >
    >
    > Allen
    >
  • On Nov 20, 2007, at 1:14 PM, Lorenzo wrote:

    > Thank you Allen, it worked like a charm. And this is a great news.
    > I can now store a lot of data in a single line. I suppose this is
    > endian-safe. It will work on Intel ad PPC. Right?
    > I mean, I can write values on Intel and read values on PPC and
    > viceversa,
    > right?

    Due to endian differences between the architectures, I would not make
    that assumption. Instead, I would swap the values from the host to
    either big-endian or little-endian before saving/sending/etc. and then
    swap them back upon loading/receiving/etc.

    Nick Zitzmann
    <http://www.chronosnet.com/>
  • If you use NSArchiver/NSUnarchiver then you'll be fine between ppc/
    intel.  Unfortunately, as noted in the recent thread "How to archive
    structs like NSRect", NSKeyedArchiver does not support coding of
    NSValues with non-trivial types.  A workaround was also proposed in
    that thread.

    dave

    On 20-Nov-07, at 1:32 PM, Nick Zitzmann wrote:

    >
    > On Nov 20, 2007, at 1:14 PM, Lorenzo wrote:
    >
    >> Thank you Allen, it worked like a charm. And this is a great news.
    >> I can now store a lot of data in a single line. I suppose this is
    >> endian-safe. It will work on Intel ad PPC. Right?
    >> I mean, I can write values on Intel and read values on PPC and
    >> viceversa,
    >> right?
    >
    >
    > Due to endian differences between the architectures, I would not
    > make that assumption. Instead, I would swap the values from the
    > host to either big-endian or little-endian before saving/sending/
    > etc. and then swap them back upon loading/receiving/etc.
    >
    > Nick Zitzmann
    > <http://www.chronosnet.com/>
    >
  • On Nov 20, 2007, at 2:50 PM, David Spooner wrote:

    > If you use NSArchiver/NSUnarchiver then you'll be fine between ppc/
    > intel.  Unfortunately, as noted in the recent thread "How to archive
    > structs like NSRect", NSKeyedArchiver does not support coding of
    > NSValues with non-trivial types.  A workaround was also proposed in
    > that thread.

    Be careful with that first statement.  It is true that if one uses
    primitive types with NSArchiver and friends, that byte-swapping is
    handled for you.  But, once you start to archive blobs of data (e.g.
    NSData containing a structure with 2-byte, 4-byte, etc. fields),
    you'll need to handle byte-swapping  yourself.

    FWIW, I've never yet used NSValue objects to wrap things for
    archiving.  I've only ever used NSData.  Not saying that's the proper/
    only solution, but it hasn't failed me yet.

    ___________________________________________________________
    Ricky A. Sharp        mailto:<rsharp...>
    Instant Interactive(tm)  http://www.instantinteractive.com
  • Yes.  I my statement was in reference to NSValue, not NSData.  Sorry
    for the confusion.

    dave

    On 20-Nov-07, at 2:42 PM, Ricky Sharp wrote:

    >
    > On Nov 20, 2007, at 2:50 PM, David Spooner wrote:
    >
    >> If you use NSArchiver/NSUnarchiver then you'll be fine between ppc/
    >> intel.  Unfortunately, as noted in the recent thread "How to
    >> archive structs like NSRect", NSKeyedArchiver does not support
    >> coding of NSValues with non-trivial types.  A workaround was also
    >> proposed in that thread.
    >
    > Be careful with that first statement.  It is true that if one uses
    > primitive types with NSArchiver and friends, that byte-swapping is
    > handled for you.  But, once you start to archive blobs of data (e.g.
    > NSData containing a structure with 2-byte, 4-byte, etc. fields),
    > you'll need to handle byte-swapping  yourself.
    >
    > FWIW, I've never yet used NSValue objects to wrap things for
    > archiving.  I've only ever used NSData.  Not saying that's the
    > proper/only solution, but it hasn't failed me yet.
    >
    > ___________________________________________________________
    > Ricky A. Sharp        mailto:<rsharp...>
    > Instant Interactive(tm)  http://www.instantinteractive.com
    >
previous month november 2007 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    
Go to today