2013-05-28 5 views
0

이 내 코드는 내가 그래서 나는이코어 데이터베이스에서 데이터를 가져 오는 방법

을이 코드를 사용하여 코어 데이터베이스로 전송 한 데이터를 가져하려는 이제 데이터를
 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]; 

를 삽입하는 것입니다 디버거가 예외가 NSKeyedUnarchiver.and에 올 때 지금
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); 

문제가이 코드를하는 것은 나에게 예외를 던지고

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 
,369입니다

아무도 나를 도와주세요 ......

답변

1

당신은 프로세스를 깨고있는 불필요한 전환을 추가하고 있습니다. NSData 개체를 저장했지만 NSString으로 읽은 다음 NSData으로 변환합니다. UTF8 변환을 실행 한 후 NSDataNSKeyedArchiver으로 보관되지 않으므로 NSKeyedUnarchiver으로 보관 취소 할 수 없습니다.

NSData을 저장 했으므로 다시 읽으십시오. NSString 거기에는 쓸모없는 것보다 더 나쁜 것이 있습니다. 또한 인코딩을 일치 시키려면 -[NSKeyedUnarchiver unarchiveObjectWithData:]을 사용해야합니다.

관련 문제