Note that there are some explanatory texts on larger screens.

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

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload