How to Retrive and Store NSArray from sqlite DB in iOS

怎甘沉沦 提交于 2019-12-23 06:03:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!