Professional Web Applications Themes

Cocoa, Object Id, encoding - decoding. - Mac Programming

Hello NG, i think that is a typical beginners question. I want to store something like a b-tree in to a file. At the moment i have a object, witch stores the a pointer to the id of the next object in a instance variable. Is there a way to store the tree so, that i don't have to rebuild the tree ( assigning the new object id's) after decoding the objects from file ? Thank you for your time an help...

  1. #1

    Default Cocoa, Object Id, encoding - decoding.

    Hello NG,

    i think that is a typical beginners question. I want to store something like
    a b-tree in to a file.
    At the moment i have a object, witch stores the a pointer to the id of the
    next object in a instance variable.
    Is there a way to store the tree so, that i don't have to rebuild the tree
    ( assigning the new object id's) after decoding the objects from file ?

    Thank you for your time an help


    Florian Guest

  2. #2

    Default Re: Cocoa, Object Id, encoding - decoding.

    In article <400fc6ab$0$24649$arcor-online.net>,
    "Florian Zschocke" <de> wrote:
     

    The easiest way is to use NSCoder. See the docs on NSCoder, NSArchiver,
    NSKeyedArchiver, NSUnarchiver, and NSKeyedUnarchiver. The short version:
    write -initWithCoder: and -encodeWithCoder: methods for every class you
    want to save to a file. (They're pretty easy to make.) Then your class
    can be encoded, so you can easily transform an arbitrary object graph to
    an NSData and vice versa using the classes I mentioned.

    There are a couple of downsides, mainly that you have no control over
    the format of the resulting file, which isn't doented, and therefore
    the files aren't portable to other APIs. If you just want to load/save
    files on the Mac, you're fine.
    Michael Guest

  3. #3

    Default Re: Cocoa, Object Id, encoding - decoding.

    Florian Zschocke wrote: 
    Persistence of objects is "build-in" with cocoa :
    You'll have to implement this two methods in your classes :
    -(void)encodeWithCoder:(NSCoder *)encoder
    -(id)initWithCoder:(NSCoder *)decoder
    (See the doentation of the NSCoding protocol).
    And use the NSArchiver to save all.

    PS : This is all explained in
    "ADC Home > Doentation > Cocoa > Data Management > Introduction to
    Archives and Serializations"
    or here
    <http://developer.apple.com/doentation/Cocoa/Conceptual/Archiving/index.html>

    --
    bruno
    Nono Guest

  4. #4

    Default Re: Cocoa, Object Id, encoding - decoding.

    Hello Bruno
    "Nono" <Nonoh.com> schrieb im Newsbeitrag
    news:40102602$0$6970$club-internet.fr...
     

    how dose that work ? I thought the object-id is a pointer to a address in
    memory ? If it is persistent it has to be unique ? I don't have my tree
    ready -i just think about the best way. So if that is true, i can have
    an object with: NSNumber * nextObject. and i will store the id of the
    following object in this var. After restoring it via NSCoder it will point
    to the same object?

    Thank you

    Florian


    Florian Guest

  5. #5

    Default Re: Cocoa, Object Id, encoding - decoding.

    In article <4011095b$0$24669$arcor-online.net>,
    "Florian Zschocke" <de> wrote:
     
    >
    > how dose that work ? I thought the object-id is a pointer to a address in
    > memory ? If it is persistent it has to be unique ? I don't have my tree
    > ready -i just think about the best way. So if that is true, i can have
    > an object with: NSNumber * nextObject. and i will store the id of the
    > following object in this var. After restoring it via NSCoder it will point
    > to the same object?[/ref]

    Don't be fooled by the name. An 'id' is just a pointer/reference, it's
    nothing magical. If you store the id in an NSNumber and then restore it,
    you almost certainly will *not* have a valid pointer; your app will
    crash. Read the docs on NSCoder and archiving. You don't need to get the
    same id back, you just need to get the same *object* back. It doesn't
    matter if your object is in the same place in memory or not, you just
    care about whether it's holds the same information.
    Michael Guest

  6. #6

    Default Re: Cocoa, Object Id, encoding - decoding.

    Hello Michael
    "Michael Ash" <com> schrieb im Newsbeitrag news:mail- 

    Ok, so how can i store the information about the nextObject in the object ?
    Otherwise I had to assign it dynamic at startup to build up the hierarchie
    of the tree - is that true ?
    The nodes can not know about their children or their parents ?

    Thank you


    Florian Guest

  7. #7

    Default Re: Cocoa, Object Id, encoding - decoding.

    In article <40111c38$0$24665$arcor-online.net>,
    "Florian Zschocke" <de> wrote:
     

    Have you read the docs on NSCoder and archiving? Please do before you
    continue this discussion.

    To encode the next object, you do this:

    [coder encodeObject:nextObject];

    It's not complicated. What the coder does behind the scenes to make
    everything work is complicated, but you don't have to do very much.
    Michael Guest

  8. #8

    Default Re: Cocoa, Object Id, encoding - decoding.

    Hello Micheal
    "Michael Ash" <com> schrieb im Newsbeitrag
    news:..
     
    Ok,ok I am too lazy sometimes and my english is worse, so it will take a
    while
    -I will post here tomorrow.

    Thank you again and have a nice weekend.

    Florian


    Florian Guest

  9. #9

    Default Re: Cocoa, Object Id, encoding - decoding.


    "Michael Ash" <com> schrieb im Newsbeitrag news:mail-> 
    Wow - if it is what it seems to be - it will be great.


    Florian Guest

  10. #10

    Default Re: Cocoa, Object Id, encoding - decoding.

    I have a position available for a C++ programmer with Mac OS 9/X experience.
    The employer is really looking hard for someone. If you're interested at all
    please drop me a line.

    Thank you

    -C


    chad Guest

Similar Threads

  1. Problem encoding/decoding image
    By Slade in forum ASP.NET General
    Replies: 2
    Last Post: January 9th, 03:49 PM
  2. Object encoding error
    By tomipont in forum Macromedia Flash Flashcom
    Replies: 2
    Last Post: March 29th, 04:03 PM
  3. [Cocoa] NSKeyedUnarchiver and "root" object
    By Manfred in forum Mac Programming
    Replies: 7
    Last Post: January 7th, 07:59 PM
  4. Replies: 0
    Last Post: September 12th, 04:42 PM
  5. Replies: 0
    Last Post: September 12th, 04:30 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139