简体   繁体   中英

How to Retrive and Store NSArray from sqlite DB in iOS

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM