0

This is my code to insert data

        NSError *error;
    AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    newtext = [NSEntityDescription insertNewObjectForEntityForName:@"TableTextView" inManagedObjectContext:context];
    //NSData *bytes = [txtView.text dataUsingEncoding:NSUTF8StringEncoding];
     NSMutableArray *arr = [[NSMutableArray alloc] init];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setObject:txtView.text forKey:@"FileString"];
    [arr addObject:dict];
    NSData *bytes1 = [NSKeyedArchiver archivedDataWithRootObject:arr];
    [newtext setValue:bytes1 forKey:@"textView"];
    [context save:&error];

Now i want to fetch the data that i have sent to core database so i used this code to do this

    NSError *errorr;
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext ];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"TableTextView" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];
    NSArray *fetchedObjects;
    fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&errorr];
    for (int i =0; i <[fetchedObjects count]; i++){
        newtext = [fetchedObjects objectAtIndex:i];
        strvalue = [NSString stringWithFormat:@"%@",[newtext valueForKey:@"textView"]];
        NSLog(@"====%@",strvalue);
        NSData *data=[strvalue dataUsingEncoding:NSUTF8StringEncoding];
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
        NSLog(@"=====%@",unarchiver);

now the problem this code throwing me exception when debugger come on the NSKeyedUnarchiver.and the exception is

2013-05-28 15:13:33.030 DocumentTouch[2376:c07] -[__NSCFData objectForKey:]: unrecognized selector sent to instance 0x8620ad0
2013-05-28 15:13:33.031 DocumentTouch[2376:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFData objectForKey:]: unrecognized selector sent to instance 0x8620ad0'
*** First throw call stack:
(0x1787012 0x15ace7e 0x18124bd 0x1776bbc 0x177694e 0x16fbe18 0xfdc9b8 0x3e62 0x15c0705 0x4f7920 0x4f78b8 0x5b8671 0x5b8bcf 0x5b7d38 0x52733f 0x527552 0x5053aa 0x4f6cf8 0x2502df9 0x2502ad0 0x16fcbf5 0x16fc962 0x172dbb6 0x172cf44 0x172ce1b 0x25017e3 0x2501668 0x4f465c 0x26dd 0x2605 0x1)
libc++abi.dylib: terminate called throwing an exception

Can anyone please help me out from this......

4

1 回答 1

1

您正在添加一个不必要的转换,这会破坏该过程。您保存了一个NSData对象,但您将其读取为NSString然后转换为NSData. 这很糟糕——因为在运行 UTF8 转换后,您会得到一个NSData未使用 存档的文件NSKeyedArchiver,因此无法使用NSKeyedUnarchiver.

您保存了一个NSData,因此只需将其作为一个读回即可。NSString有比没用更糟糕的。此外,您应该使用-[NSKeyedUnarchiver unarchiveObjectWithData:], 来匹配您的编码。

于 2013-05-28T18:52:51.727 回答