0

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

2
  • Can you show what you are storing in itemQtyArray? Commented May 3, 2013 at 9:26
  • HI anup i am storing integer values only like 2,3 ,5 Commented May 3, 2013 at 9:54

1 Answer 1

3
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

Sign up to request clarification or add additional context in comments.

21 Comments

Thanks Divz, but how should i covert array to nsdata , only thing i found here using Archiver i.e i used that. can you please explain how should i do that , i have n to much exp in iOS
@Sulabh i have already mentioned for the array please have a look.
@Sulabh for more details have a look of soff.es/archiving-objective-c-objects-with-nscoding
Are you using NSArray or NSMutableArray if NSArray then please use NSMutableArray
@Sulabh If you array contains any custom objects, you need to implement NSCoding protocol, and have to serialize while storing the array into sqlite as data and deserialize back to array. The suggestion of Divz is a valid one. If you are finding it difficult, try using FMDB. They are providing a good wrapper around the native c based sqlite provided by iOS.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.