简体   繁体   English

如何在iOS中从sqlite DB检索和存储NSArray

[英]How to Retrive and Store NSArray from sqlite DB in iOS

I have an array itemQtyArray which i am storing in SQLITE table As: 我有一个数组itemQtyArray,它存储在SQLITE表中,如:

    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: 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:

    '-[__NSCFData objectForKey:]: unrecognized selector sent to instance

Please tell what is wrong 请告诉我出了什么问题

NSData *toData = [NSKeyedArchiver archivedDataWithRootObject:array];

instead of this you should pass NSDictionary 而不是这个,您应该通过NSDictionary

NSData *toData = [NSKeyedArchiver archivedDataWithRootObject:dictionary];

or 要么

o convert a generic array to an NSData, you need an archiver! o将通用数组转换为NSData,您需要一个存档器! If you know how to feed the NSData, you know how to use NSKeyedArchiver. 如果您知道如何提供NSData,那么您将知道如何使用NSKeyedArchiver。 So: 所以:

NSArray* array= ... ;
NSData* data=[NSKeyedArchiver archivedDataWithRootObject:array];

Of course all elements in your array needs to implement NSCoding protocol 当然,数组中的所有元素都需要实现NSCoding协议

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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