Vipin
Vipin

Reputation: 4728

problem with converting NSData to NSArray

Hi I have a problem with converting NSData to NSArray

my code is:

NSData *data = [[NSData alloc] initWithBytes:(const void *)buf length:len];
NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:data];

can any one help me to do it.

Upvotes: 2

Views: 1980

Answers (2)

Tariq
Tariq

Reputation: 9979

Use +[NSPropertyListSerialization dataWithPropertyList:format:options:error:] to convert the data, then check if the result -isKindOfClass:[NSArray class].

It will work :)

Upvotes: 1

dawg
dawg

Reputation: 103694

I have no idea what is inside your byte buffer. This code works for a simple buffer of character bytes.

Try:

char buf[]="123456";
NSData *bufObj=[NSData dataWithBytes:(const void *)buf length:sizeof buf];
if(bufObj==nil)
    NSLog(@"failed to create obj");
else {
    NSMutableArray *marr=[NSMutableArray array];
    [marr addObject:bufObj];
    NSArray *arr=[NSArray arrayWithObject:bufObj];

    NSLog(@"test:\n\tbufObj: %@\n\tmarr: %@\n\tarr: %@",bufObj,marr,arr);
}

Upvotes: 1

Related Questions