BaQiWL
BaQiWL

Reputation: 437

How to make a .plist file

I have an api inside are the city's ID, I do not want to request data every time,I want to writeToFile as plist,but the first written is too slow and memory skyrocketing. Is there any method I can use to make it into a plist as local file,so users do not have to write again

Plist file obtained in this way is no data on the xcode view, but in fact it has already been written, you can find data through code

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *plistPath = [paths objectAtIndex:0];
NSMutableArray *marr = [NSMutableArray array];
NSString *filename=[plistPath stringByAppendingPathComponent:@"city.plist"];
NSLog(@"filename == %@",filename);
[marr addObject:@"字符串"];
[marr writeToFile:filename atomically:YES];

Upvotes: 0

Views: 1077

Answers (5)

BaQiWL
BaQiWL

Reputation: 437

this method is very useful, thanks for evervone,thanks for rmaddy

do{
       let data = try NSPropertyListSerialization.dataWithPropertyList(response.result.value as! NSMutableDictionary, format: NSPropertyListFormat.XMLFormat_v1_0, options: 0)

       data.writeToFile(countryListPath, atomically: true)
   }catch
  {

  }

Upvotes: 0

MD Aslam Ansari
MD Aslam Ansari

Reputation: 1565

You can create a property list in Objective-C if all of the objects in the aggregate derive from the NSDictionary, NSArray, NSString, NSDate, NSData, or NSNumber class.

Use following code:

//Get the documents directory path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"plist.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath: path]) {

    path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat:@"plist.plist"] ];
}

NSMutableDictionary *data;

if ([fileManager fileExistsAtPath: path]) {

    data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
}
else {
    // If the file doesn’t exist, create an empty dictionary
    data = [[NSMutableDictionary alloc] init];
}

//To insert the data into the plist
[data setObject:@"iPhone 6 Plus" forKey:@"value"];
[data writeToFile:path atomically:YES];

//To retrieve the data from the plist
NSMutableDictionary *savedValue = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
NSString *value = [savedValue objectForKey:@"value"];
NSLog(@"%@",value);

For more details click here

Apple has also put a demo project for creating plist file here.

Upvotes: 1

Pritesh Raiyani
Pritesh Raiyani

Reputation: 313

If you are about to create Plist without programmatically then follow these steps :

  1. Right Click on Files and Select 'New File...' option.
  2. Choose Resources from OS X tab.
  3. An option for Property List is available.
  4. Select an give an appropriate name.

This gets added to your project.

Upvotes: 2

rptwsthi
rptwsthi

Reputation: 10172

Following function take serilizable object(dictionary, array) as input and convert it into plist and write it in document disrectory of application:

+ (BOOL) writeBundelPlist : (NSString *) plistName with : (id) newPlistData {
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:plistName];
    if (![fileManager fileExistsAtPath:plistPath]) {
        NSString *bundle = [[NSBundle mainBundle] pathForResource:[plistName stringByDeletingPathExtension] ofType:[plistName pathExtension]];
        NSLog(@"plistPath = %@", plistPath);
        [fileManager copyItemAtPath:bundle toPath:plistPath error:&error];
    }
    return [newPlistData writeToFile:plistPath atomically:YES];
}

Upvotes: 0

Mortgy
Mortgy

Reputation: 563

I wrote a class that caches API responses, so later when u request Countries endpoint, it will check defined endpoints to cache, if it find it in list and it exist in cache, it will return the cached one and will not complete performing the HTTP request, in docs i provided steps how to implement it.

MGCacheManager

Upvotes: 0

Related Questions