问题
I have an array itemQtyArray which i am storing in SQLITE table As:
NSData *toData = [NSKeyedArchiver archivedDataWithRootObject:self.itemQtyArray];
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO ORDERTABLE(ITEMDESC,ITEMQTY)VALUES( \"%@\",\"%@\");",dataString2,toData];
and then i am retrieving as :
const void *itemQty = sqlite3_column_text(statement, 2);
NSInteger qtyBytes = sqlite3_column_bytes(statement, 2);
NSData *data2 = [NSData dataWithBytes:itemQty length:qtyBytes];
NSArray *myArrayFromDB = [[NSArray alloc]init];
myArrayFromDB = [NSKeyedUnarchiver unarchiveObjectWithData:data2];
[self.itemQtyArray addObjectsFromArray:myArrayFromDB];
BUt i am getting Exception at line
myArrayFromDB = [NSKeyedUnarchiver unarchiveObjectWithData:data2];
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'-[__NSCFData objectForKey:]: unrecognized selector sent to instance
Please tell what is wrong
回答1:
NSData *toData = [NSKeyedArchiver archivedDataWithRootObject:array];
instead of this you should pass NSDictionary
NSData *toData = [NSKeyedArchiver archivedDataWithRootObject:dictionary];
or
o convert a generic array to an NSData, you need an archiver! If you know how to feed the NSData, you know how to use NSKeyedArchiver. So:
NSArray* array= ... ;
NSData* data=[NSKeyedArchiver archivedDataWithRootObject:array];
Of course all elements in your array needs to implement NSCoding protocol
来源:https://stackoverflow.com/questions/16355079/how-to-retrive-and-store-nsarray-from-sqlite-db-in-ios