Getting Error when UnArchived NSMutableDictionary o NSData?

832 views Asked by At

I am trying to send a dictionary of information from one iphone to another iphone through wifi/blutooth,for that i implement CFnetwork and NSNetservice Concept.I received Data in the Reciver side.

I did code as follows ....

**Sender Side**

 samDict=[NSMutableDictionary dictionary];
     [samDict setObject:@"Muhammed Musthafa" forKey:@"PP"];
     [samDict setObject:@"John P" forKey:@"Jose"];
     [samDict setObject:@"Lubaba" forKey:@"P"];
     [samDict setObject:@"JINI" forKey:@"KS"];
     [samDict setObject:@"Anusha" forKey:@"GS"];
     [samDict setObject:@"Athulya" forKey:@"V"];
     [samDict setObject:@"Riyas" forKey:@"MM"];
     [samDict setObject:@"Raihanath" forKey:@"MH"];
     [samDict setObject:@"Shabeer" forKey:@"poola"];
     [samDict setObject:@"Rajisha" forKey:@"Raji"];

     //NSLog(@" Dictionary values.............%@",samDict);

    NSMutableData *ArchiveData=[[NSMutableData alloc]init];    
    NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:ArchiveData];
    [archiver setOutputFormat: NSPropertyListXMLFormat_v1_0];

    [archiver encodeObject:self.samDict forKey:@"Some Key Values"];
    [archiver finishEncoding];    
    [archiver release];

    const uint8_t *buffer=(const uint8_t *)[ArchiveData bytes];
    NSInteger nWrtitten=[_outStream write:buffer maxLength:[ArchiveData legth]];
    if (!nWrtitten)
    {
        NSLog(@"Eorror writting to Stream %@ : %@", _outStream, [_outStream streamError]);
    }
    else
    {

        NSLog(@" Write %ld bytes to Stream %@",(long)nWrtitten, _outStream);
    }

Receiver side

 NSInteger bytesRead;
        uint8_t buffer[13000];
        NSMutableData *data=[[NSMutableData data]retain];           

        if (stream==_inStream)
        {


            bytesRead=[_inStream read:buffer maxLength:sizeof(buffer)];
            if (bytesRead==-1&& bytesRead==0)
            {
                NSLog(@"_inStream Error...................");
            }
            else
            {                                       
                [data appendBytes:buffer length:bytesRead];
                NSLog(@"Data Received has length   :%d\n\n",[data length]);

                NSLog(@"data............%@",data);

            }
        }

I got data in Receiver Side.that is in bytes format . I can't UnArchive this data in Receiver side.if i am trying to unarchived this to Dictionary format like this

NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
   NSMutableDictionary* UnArchiveDictionary =[NSMutableDictionary dictionary];
    UnArchiveDictionary = [[unarchiver decodeObjectForKey:@"Some Key Values"] retain]; 
    NSLog(@"UnArchive Dictionary  %@",UnArchiveDictionary);  

But unfortunately ican't Unarchived this data in Dictionary Format. i am getting Error of line NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];

I have found some code for that converting bytes Data to String Format like this

size_t length=[data length];
                    unsigned char aBuffer[length];
                    [data getBytes:aBuffer length:length];
                    aBuffer[length - 1]=0;
                    NSString *messageString =aBuffer; 

                    NSLog (@"%s",messageString);

But my problem is how to get back the same dictionary . Why getting error in Unarchived time?

1

There are 1 answers

5
DarkDust On BEST ANSWER

The problem might be that the condition:

if (bytesRead==-1&& bytesRead==0)

will always be false. A variable can't have two different values at once. You probably want:

if (bytesRead==-1 || bytesRead==0)

So if there was an error (or no data was transmitted) your code wouldn't notice and instead append garbage bytes from the stack to your mutable data.

The keyed unarchiver then tries to interpret these garbage bytes as an archive and notices that it isn't, so it throws an exception.