How to Retrive and Store NSArray from sqlite DB in iOS - 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

Related

Assigning text to UILabel gives error after parsing JSON

I've the following code for receiving response from PHP web service in JSON Format:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *responseStringWithEncoded = [[NSString alloc] initWithData: mutableData encoding:NSUTF8StringEncoding];
NSLog(#"Response from Server : %#", responseStringWithEncoded);
[self getData:responseStringWithEncoded];
}
-(void) getData:(NSString *) responseStringWithEncoded{
NSData *data = [responseStringWithEncoded dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(#"object for key ime = %#", [json objectForKey:#"imei"]);
NSString * jimei = [json objectForKey:#"imei"];
NSLog(#"jimei = %#", jimei);
// NSDictionary * jimei = [json objectForKey:#"imei"];
imeiLable.text = jimei;
}
I am successfully retrieving data in simulator but when assigning one value among received string(NSDictionary) to imeiLable.text it gives following error.
Here is the output:
Request data = { URL: http://localhost/getjsonimei.php?imei=478593219801234 }
Response from Server : {"id":7,"imei":478593219801234,"mname":"Samsung Glaxy","pamount":"2000 rupees","pname":"Faizi","address":"House number 88, block 31","cnumber":11122233,"nic":"87456893"}
object for key ime = 478593219801234
jimei = 478593219801234
Here is detailed description of simulator resulting string(dictionary) and error.
2017-05-24 20:01:13.229 imiechecker[4005:61372] -[__NSCFNumber length]: unrecognized selector sent to instance 0xb01b3472adbb0923
2017-05-24 20:01:13.263 imiechecker[4005:61372] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0xb01b3472adbb0923'
I also tried following approach:
NSDictionary * jimei = [json objectForKey:#"imei"];
but having the same error.
Please suggest where I am doing it wrong?
Your IMEI number is of data type NSNumber, not NSString.
Try this:
NSString * jimei = [NSString stringWithFormat:#"%#", [json objectForKey:#"imei"]];
imeiLable.text = jimei;

JSON Parsing in Xcode returning error

I'm trying to parse the following JSON information that comes out of my PHP file:
{
"netSales":0,
"voidSales":0,
"discountSales":0,
"guestCount":null,
"servedCount":null,
"loggedIn":9
}
My string is set something like this:
NSURL * url = [NSURL URLWithString:salesStr];
NSData * data = [NSData dataWithContentsOfURL:url];
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSLog(#"response type is %#",[json class]);
//Set up our cities array
arrayOfStore = [[NSMutableArray alloc]init];
for (int i = 0; i < json.count; i++)
{
NSString * netSales = json[i][#"netSales"];
NSString * voids = json[i][#"voidSales"];
NSString * discounts = json[i][#"discountSales"];
NSString * guestCount = json[i][#"guestCount"];
NSString * peopleServed = json[i][#"servedCount"];
NSString * employeesClock = json[i][#"loggedIn"];
Store * myStore = [[Store alloc]initWithNetSales: (NSString *) netSales andVoids: (NSString *) voids andDiscounts: (NSString *) discounts andGuestCount: (NSString *) guestCount andPeopleServed: (NSString *) peopleServed andEmployeesClock: (NSString *) employeesClock];
[arrayOfStore addObject:myStore];
But it's returning the error message:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x8ddb930'
What does this error mean?
EDIT: I extended my code a bit.
Your error message, "[__NSCFDictionary objectAtIndex:]: unrecognized selector sent", is informing you that you attempted to call objectAtIndex (an array method) on an object that was really a dictionary.
Your code snippet appears to assume that the JSON is an array of dictionaries. But on the basis of what you've shared with us, it looks like a simple dictionary. That is consistent with the error message you received. You could remedy this by just calling objectForKey, and eliminate the call to objectAtIndex.
By the way, netSales appears to be a number, not a string, so use NSNumber rather than NSString.
So, putting those together, I think you'd want:
NSNumber *netSales = json[#"netSales"]; // or [json objectForKey:#"netSales"];

How to parse integer value to Json iOS?

I was passed NSstring it is coming properly data into uitableview but when I am parsing integer value it showing exception Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: -[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x744e9d0
This is my code
(void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData options:kNilOptions error:&error];
NSArray * values=[json objectForKey:#"loans"];
NSLog(#"Array: %#",values);
for (NSDictionary *file in values)
{
NSNumber *fileTitle=[file objectForKey:#"id"];
// NSString *fileTitle = [file objectForKey:#"sector"];
[titles addObject: fileTitle];
}
[self.mainTableView reloadData];
}
You are assigning string value to NSNumber type.
Try to use this
NSString *fileTitle=[file objectForKey:#"id"];
[titles addObject: fileTitle];
and use string value.
[[titles addObject: fileTitle] stringValue];
I guess you are doing something like this in your -tableView: cellForIndexPath:
UITableViewCell *cell = [...];
cell.titleLabel.text = titles[indexPath.row];
You can only use NSString for -[UILabel setText:] method.
Convert your NSNumber to NSString with a simple -[NSNumber stringValue] or using NSNumberFormatter.
Everything seems ok. Just replace
NSNumber *fileTitle=[file objectForKey:#"id"];
by NSString *fileTitle = [file objectForKey:#"id"];

Objective-c read image from sqlite

NSData *imageData = UIImageJPEGRepresentation(PhotoImage, 100);
NSString* queryString = ;
[SQLiteAccess insertWithSQL:
[NSString stringWithFormat:#"insert into images (photo) values ('%#')", imageData] // photo - blob column
]; // i use class SQLiteaccess
insert is normally
but when i read image from sqlite
NSArray *photoSelectArray = [SQLiteAccess selectManyRowsWithSQL: [NSString stringWithFormat:#"select photo from places where id=%i", idPlace]];
NSDictionary *imageDataDic = [photoSelectArray objectAtIndex:0];
NSData *dataForCachedImage = [[NSData alloc] initWithData: [imageDataDic objectForKey:#"photo"]];
UIImage *cachedImage = [UIImage imageWithData:dataForCachedImage];
i have an error - SIGABRT (Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString bytes]: unrecognized selector sent to instance 0x1e5c46d0')
p.s. class for easy access to sqlite
- SQLiteAccess.h - http://pastebin.com/BFxFry7T
- SQLiteAccess.m - http://pastebin.com/m67yNVLm
Yeah, I think the problem is here:
[NSString stringWithFormat:#"insert into images (photo) values ('%#')", imageData]]; // i use class SQLiteaccess
When using SQLite directly, you should set the binary data by sqlite3_bind_blob(), but you're actually setting the binary data as string.
So when you get back the data, it's not a binary data, it's a String object, of course, an string object don't response to -[NSData bytes]
Meanwhile I think you should check the field type in your SQLite DB file. Does the photo is actually set to blob.

Get Row from NSArray

Hello i get a json that looks like this:
features: (
{
attributes = {
Gecontroleerd = Ja;
};
geometry = {
x = "5.968097965285907";
y = "52.50707112779077";
};
}
)
From this code:
NSDictionary *root = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
NSArray *data = [root objectForKey:#"features"];
NSLog(#"features: %#", data );
for (NSArray *row in data) {
NSString *latitude = row[5];
NSString *longitude = row[7];
NSString *crimeDescription = #"test";
NSString *address = #"banaan";
And u need to x values 5.968097965285907 for latitude
and y values 52.50707112779077 for longitude
But i get this error:
[__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x14831450
2012-11-14 10:10:59.000 ArrestPlotter[6330:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x14831450'
*** First throw call stack:
(0x1860012 0x1655e7e 0x18eb4bd 0x184fbbc 0x184f94e 0x2dbc 0x3b8f 0x1cc1e 0x16696b0 0xfa0035 0x17e3f3f 0x17e396f 0x1806734 0x1805f44 0x1805e1b 0x224a7e3 0x224a668 0x4a765c 0x25bd 0x24e5 0x1)
libc++abi.dylib: terminate called throwing an exception
(lldb)
Does anyone wich row i need to select?
I guess that the only thing is that the row number needs to be changed. Or maybe there should be something like this : [1][5]. Im not quite sure how this works
NSArray *data = [root objectForKey:#"features"];
NSLog(#"features: %#", data );
for (NSDictionary *dic in data) {
NSDictionary geometry = [dic objectForKey:#"geometry"];
// Do what you want..
NSString *myAwesomeX = [geometry objectForKey:#"x"];
NSString *myAwesomeY = [geometry objectForKey:#"y"];
}
The problem here is that you are trying to send a selector message to object row, that is in memory a NSDictionary (NSCFDictionary?) object, and you are trying to manage it like a NSArray.
The method objectAtIndexedSubscript (is underlying called by row[5] and row[7]) exists in NSDictionary, but no in NSArray.
Change
for (NSArray *row in data) {
by
for (NSDictionary *row in data) {
Also, you have to change the management of data inside for, look at the result of your log statement and act accord whit it.
I hope this will help!

Resources