I have this simple json file in my iphone/ipad project.
{"initial_page" : Page_Selected}
Page_Selected is an NSInteger
I want to modify through a UIPickerView the value of the "initial_page". How to do that?
of course I’ve already done the UiPickerWiew selection part and about the json modification, I will write it into the
-(void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponents:(NSInteger)component{ }
function.
Maybe function that does the job is [[jsonParsed setInteger:row forKey:@"initial_page"] but this way, the debugger returns me “unrecognized selector sent” exception.
I need only the part in which to modify the json parameter with "initial_page" as the key.
anyone can help me?
that’s what I got.
NSError *jsonParsingError = nil;
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Config" ofType:@"json"];
NSMutableDictionary *configJson = [NSJSONSerialization
JSONObjectWithData:[NSData dataWithContentsOfFile:filePath]
options:NSJSONReadingMutableContainers error:&jsonParsingError];
[configJson setValue:[NSNumber numberWithInt:row] forKey:@"initial_page"];
NSLog(@"%@",jsonParsingError);
the last line returns me (null), so the json file is ok.
BUT, If I print out [configJson objectForKey:@"inital_page"] then I can see it changes… so, is there a kind of “commit” or “save” command in order to make it persistent?
following suggestions:
NSData * modified = [NSJSONSerialization dataWithJSONObject:configJson options:NSJSONWritingPrettyPrinted error:&jsonParsingError];
[modified writeToFile:filePath options:NSDataWritingAtomic error:&jsonParsingError];
NSLog(@"returned error: %@", [jsonParsingError localizedDescription]);
again, last line returns me (null) but I can’t find any modification in the file
after changes, I can save the modification of the initial_page, but this works only on iPhone/iPad Simulator and not on the real device debugging.
here’s what I do:
if([modified wirteToFile:filePath options:NSDataWritingFileProtectionNone error:&jsonParsingError])
NSLog(@"writing ok")
else
NSLog(@"not ok with error :%@",jsonParsinError);
Iphone simulator has no problem with the last 4 lines, but when I run it on physical iPhone, the last NSLog gives:
"not ok with error : Error Domain=NSCocoaErrorDomain Code=513 The Operation Could not be completed. Operation not permitted"
it seems a permission denied in writing the json file.
the problem is solved: the main wrong concept is to write and / or modify files in the main bundle. now I do this in the
NSDocumentDirectorythis way, I get no error and I can read/write the data directly in/from the iPhone.
I followed this guide: link of course I use json as filter instead pdf.