Search code examples
iosobjective-cjsonnsdata

How to save infos as Json


I have some infos to show in couple of labels on the screen such as time, coordinate of buttons, total video record time..
Instead of showing some infos in a label? How to save these infos locally in memory using maybe a json file?

- (IBAction)firstButton:(UIButton *)sender {
sender.selected  = !sender.selected;
[self starting];
[self allButtonsSelected];

_startTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                               target:self
                                             selector:@selector(firtBtnCount)
                                             userInfo:nil
                                              repeats:YES];

self.lblForInfo.text = NSStringFromCGRect(_firstButton.frame);
NSLog(@"STOP RECORDING");
    [_startTimer invalidate];

Solution

  • you can do

    NSMutableArray *dicArray = [[NSMutableArray alloc]init];
    
    [dicArray addObject:@{@"frame":NSStringFromCGRect(button1.frame)}];
    [dicArray addObject:@{@"frame":NSStringFromCGRect(button2.frame)}];
    [dicArray addObject:@{@"frame":NSStringFromCGRect(button3.frame)}];
    [dicArray addObject:@{@"frame":NSStringFromCGRect(button4.frame)}];
    [dicArray addObject:@{@"frame":NSStringFromCGRect(button5.frame)}];
    
    NSLog(@"dicArray:%@",dicArray);
    
    NSData *data = [NSJSONSerialization dataWithJSONObject:dicArray options:kNilOptions error:nil];
    
    NSArray *retDicArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    
    NSLog(@"retDicArray:%@",retDicArray);