struct into a pasteboard

  • Hi,
    I have to put this struct into a pasteboard.
    Since it contains an NSString it seems that I cannot convert it to an
    NSValue nor NSData in one shot only. Of course I know that I can put any
    value manually into an NSDictionary. The problem is that if I modify the
    struct I wouldn't like to go and modify the dictionary. I would like to get
    this done automatically. Is a way to do that?

    typedef struct
    {
        NSString    *mapPath;

        BOOL        mapOn;
        int        mapFillMode;
        NSRect      mapRect;

        NSSize      mapTile;
        float      mapTileGap;
        BOOL        mapTileGapExternal;
        float      mapAngle;
        NSSize      mapScale;

        /////////// 30 values more....

    } PKTex;

    Best Regards
    --
    Lorenzo
    email: <archidea...>
  • I'm not aware of any way of keeping a dictionary automatically
    synchronized with a struct. If you think the struct is likely to be
    modified after put it on the pasteboard but before it is retrieved
    from the pasteboard, you might want to use the "Lazy Writing" pattern,
    described in http://developer.apple.com/documentation/Cocoa/Conceptual/CopyandPaste/Arti
    cles/pbImplementing.html

    .

    This doesn't solve the problem of serializing your struct into an
    NSData, but it might address your modification problem.

    So, I'm burning to know - why not just use a mutable dictionary
    instead of a struct? (Not telling you how to write your app, just
    curious)

    Wil

    On Dec 22, 2007, at 11:46 AM, Lorenzo wrote:

    > Hi,
    > I have to put this struct into a pasteboard.
    > Since it contains an NSString it seems that I cannot convert it to an
    > NSValue nor NSData in one shot only. Of course I know that I can put
    > any
    > value manually into an NSDictionary. The problem is that if I modify
    > the
    > struct I wouldn't like to go and modify the dictionary. I would like
    > to get
    > this done automatically. Is a way to do that?
    >
    >
    > typedef struct
    > {
    > NSString    *mapPath;
    >
    > BOOL        mapOn;
    > int        mapFillMode;
    > NSRect      mapRect;
    >
    > NSSize      mapTile;
    > float      mapTileGap;
    > BOOL        mapTileGapExternal;
    > float      mapAngle;
    > NSSize      mapScale;
    >
    > /////////// 30 values more....
    >
    > } PKTex;
    >
    >
    >
    >
    > Best Regards
    > --
    > Lorenzo
    > email: <archidea...>
  • On Dec 22, 2007, at 11:46 AM, Lorenzo wrote:
    > I have to put this struct into a pasteboard.
    > Since it contains an NSString it seems that I cannot convert it to an
    > NSValue nor NSData in one shot only. Of course I know that I can put
    > any
    > value manually into an NSDictionary. The problem is that if I modify
    > the
    > struct I wouldn't like to go and modify the dictionary. I would like
    > to get
    > this done automatically. Is a way to do that?

    A couple of comments:

    - why isn't this a subclass of NSObject?  The memory overhead will be
    4 bytes for the isa pointer and you can still treat it as a struct if
    you want (foo->mapOn, for example).

    - you can use NSArchiver/NSUnarchiver to shove the data onto the
    pasteboard.  You don't need to have a root object to do so.  But
    going this route would be easier if it was just a subclass of NSObject.
  • I think that what Lorenzo wants to do is to put pointer(s) on the
    pasteboard, so he can change their values.  Pointers on the pasteboard
    don't work.  (I've been down that road).

    If your dragging source and destination are both within your app,
    consider the "sneakerboard" method.  Instead of putting your struct
    into the pasteboard, put into a global variable, your "sneakerboard".
    To the pasteboard, write some nonsense object, or maybe an NSString
    which would be appropriate for other apps receiving your drags.  Then,
    when your drag destination receives a drag of your type, ignore the
    pasteboard data and instead grab the value from your sneakerboard.
  • Yes Bill,
    I have made it right that way. Thanks.

    Best Regards
    --
    Lorenzo
    email: <archidea...>

    > From: Bill Bumgarner <bbum...>
    > Date: Sat, 22 Dec 2007 12:20:27 -0800
    > To: Lorenzo <archidea...>
    > Cc: <cocoa-dev...>
    > Subject: Re: struct into a pasteboard
    >
    > On Dec 22, 2007, at 11:46 AM, Lorenzo wrote:
    >> I have to put this struct into a pasteboard.
    >> Since it contains an NSString it seems that I cannot convert it to an
    >> NSValue nor NSData in one shot only. Of course I know that I can put
    >> any
    >> value manually into an NSDictionary. The problem is that if I modify
    >> the
    >> struct I wouldn't like to go and modify the dictionary. I would like
    >> to get
    >> this done automatically. Is a way to do that?
    >
    > A couple of comments:
    >
    > - why isn't this a subclass of NSObject?  The memory overhead will be
    > 4 bytes for the isa pointer and you can still treat it as a struct if
    > you want (foo->mapOn, for example).
    >
    > - you can use NSArchiver/NSUnarchiver to shove the data onto the
    > pasteboard.  You don't need to have a root object to do so.  But
    > going this route would be easier if it was just a subclass of NSObject.
    >
    >
    >
previous month december 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
31            
Go to today