NSMutableArray to NSData

  • Hello again.

    Quick question: I need to convert NSMutableArray into an NSData object. I
    am planning to do that my writing the NSMutableArray to a file in /tmp and
    then reading the contents of the file in the NSData. Finally I will unlink
    the /tmp file.

    This is a pretty stupid approach to solving my problem. Is there anything
    better out there?

    Nick
  • recursivecall,

    >>>>>> <recursivecall...> (r) wrote at Sun, 6 Jan 2002 09:45:01 -0500:
    r> This is a pretty stupid approach to solving my problem. Is there anything
    r> better out there?

    Check the NSArchiver documentation -- you can archive directly into NSData.
    ---
    Ondra Cada
    OCSoftware:    <ocs...>              http://www.ocs.cz
    2K Development: <o.cada...>  http://www.2kdevelopment.cz
    private        <ondra...>            http://www.ocs.cz/oc
  • On Sunday, January 6, 2002, at 03:45 , <recursivecall...> wrote:

    > Quick question: I need to convert NSMutableArray into an NSData object.
    > I am planning to do that my writing the NSMutableArray to a file in
    > /tmp and then reading the contents of the file in the NSData. Finally I
    > will unlink the /tmp file.
    >
    > This is a pretty stupid approach to solving my problem. Is there
    > anything better out there?

    NSData *myData=[NSArchiver archivedDataWithRootObject:myMutableArray];

    andy
  • Le dimanche 6 janvier 2002, ` 03:45 , <recursivecall...> a icrit :

    > Hello again.
    >
    > Quick question: I need to convert NSMutableArray into an NSData object. I
    > am planning to do that my writing the NSMutableArray to a file in /tmp
    > and then reading the contents of the file in the NSData. Finally I will
    > unlink the /tmp file.
    >
    > This is a pretty stupid approach to solving my problem. Is there anything
    > better out there?
    >
    >

    If you just want to encode the array, that is, the pointers, but not the
    objects themselves, you can use the following code:

    @implementation NSArray (dataConversion)

    /** Convert to NSData, NOT encoding the including objects (only pointers)
    */
    - (NSData*) convertToData {
    unsigned n= [self count];
    NSMutableData* data = [NSMutableData dataWithLength: sizeof(unsigned)+
      sizeof(id) *n];
    unsigned* p = [data mutableBytes];
    *p++= n;
    [self getObjects:(void*)p];
    return data;
    }

    /** Reciprocal of convertToData */
    + (NSArray*) arrayWithData:(NSData*) data {
    unsigned* p = (unsigned*)[data bytes];
    unsigned n = *p++;
    return [NSArray arrayWithObjects:(id*)p count:n];
    }

    @end

    Otherwise use NSArchiver (make sure that all objects in the array conform
    to NSCoding).

    Thomas Lachand-Robert
    ********************** <tlr...>
    << Et le chemin est long du projet ` la chose. >> Molihre, Tartuffe.
previous month january 2002 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