这是我插入数据的代码
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];
现在我想获取已发送到核心数据库的数据 所以我用这段代码来做这个
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);
现在问题是这个代码在调试器出现在NSKeyedUnarchiver上时抛出异常而且异常是
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
任何人都可以帮助我......
答案 0 :(得分:1)
您正在添加一个不必要的转换,这会破坏整个过程。您保存了NSData
个对象,但是您将其视为NSString
,然后将其转换为NSData
。这很糟糕 - 因为在完成UTF8转换后,您得到的NSData
未使用NSKeyedArchiver
归档,因此无法使用NSKeyedUnarchiver
取消归档。
您保存了NSData
,所以请将其作为一个阅读。 NSString
比无用更糟糕。此外,您应该使用-[NSKeyedUnarchiver unarchiveObjectWithData:]
来匹配您的编码。