How to make a .plist file

Report
Question

Please briefly explain why you feel this question should be reported .

Report Cancel

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];
solved 0
5 Answer 15 views 0

Answers ( 5 )

    1
    January 12, 2017 at 10:00 am

    Please briefly explain why you feel this answer should be reported .

    Report Cancel

    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.

    Best answer
    0
    January 12, 2017 at 10:00 am

    Please briefly explain why you feel this answer should be reported .

    Report Cancel

    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

    0
    January 12, 2017 at 10:00 am

    Please briefly explain why you feel this answer should be reported .

    Report Cancel

    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];
    }
    
    0
    January 12, 2017 at 10:00 am

    Please briefly explain why you feel this answer should be reported .

    Report Cancel

    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{
    
                        }
    
    0
    January 12, 2017 at 10:00 am

    Please briefly explain why you feel this answer should be reported .

    Report Cancel

    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.

Leave an answer

Browse

What is the capital of Egypt ? ( Cairo )

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>