IOS development learning – plist file

Time:2021-7-28

1. Plist file overview

It is not reasonable to write the data directly in the code. If the data is changed frequently, the corresponding code must be opened frequently for modification, resulting in low code scalability.

Therefore, it can be considered to store the frequently changed data in the file, and read the latest data from the file after the program starts. If you want to change the data, you can directly modify the data file without modifying the code.

Generally, attribute list files can be used to store data such as nsarray or nsdictionary. The extension of this attribute list file is plist, so it is also called “plist file”.

2. Create plist file

When creating a plist file, pay special attention that the name cannot contain “info”. If you bring “info”, Xcode will mistakenly treat it as a file in a project., The file cannot be loaded.

IOS development learning - plist file

3. Parsing plist files

(1) Get the full path of the plist file

NSBundle *bundle = [NSBundle mainBundle];
NSString *path =
[bundle pathforresource: @ "imagedata (file name)" oftype: @ "plist (extension)"];

(2) Load plist file

//If you load from the network, you can use:
//_images = [NSArray arrayWithContentsOfUrl:url];
 _images = [NSArray arrayWithContentsOfFile:path];
 - (NSArray *)images
{
  if (_images == nil)
  {
   //You can use mainbundle to get any resources in the phone
   NSBundle *bundle =  [NSBundle mainBundle];
   NSString *path =
[bundle pathForResource:@"imageData" ofType:@"plist"]; self.imageData = [NSArray arrayWithContentsOfFile:path];
 }

  return _images; 
}

Print the path of the project:

//Get the path of the project
NSBundle *bundle = [NSBundle mainBundle];
NSLog(@"%@", bundle);

IOS development learning - plist file

Obtain the plist file path under the project directory

//Obtain the plist file path under the project directory
 NSString *path = [bundle pathForResource:@"images" ofType:@"plist"];
    NSLog(@"%@", path);

IOS development learning - plist file

Plist add data:
Root is an array type, which contains two dictionaries

IOS development learning - plist file

Full code:

//
//  ViewController.m
//02 plist learning
//
//  Created by kaiyi wang on 16/8/25.
//  Copyright  ©  2016 corwien. All rights reserved
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    //1. Obtain the full path of plist file
    NSBundle *bundle = [NSBundle mainBundle];
    
    NSString *path = [bundle pathForResource:@"images" ofType:@"plist"];
    // NSLog(@"%@", path);
    
    NSArray *myPlist = [NSArray arrayWithContentsOfFile:path];
    NSLog(@"%@", myPlist);   //  Print plist
    
    // NSNumber *num = @0;
    //Gets the first element of the array, dictionary type
    NSDictionary *mydict = [myPlist objectAtIndex:0];   
    NSLog(@"%@", mydict[@"name"]);
    
}


@end

Print results:

IOS development learning - plist file

The plist file is converted into an nsarray, which stores various dictionaries. Later, you can retrieve the corresponding dictionary from the dictarray according to the index, and then retrieve the corresponding data according to the field.