iOS Development plist file operations

Source: Internet
Author: User

iOS Development plist file operations
    • Browse:6287
    • |
    • Updated: 2015-02-05 19:57
    • 1
    • 2
    • 3
    • 4
    • 5
Step through Reading

iOS development common data storage methods are: Nskeyedarchiver, nsuserdefaults, write write mode, SQLite. It is necessary to store data in a file format for simple and straightforward storage and visualization of data. plist files are write-written in iOS development and can be displayed as a list of property lists or in XML format. It is convenient for data management. It is necessary to master the use of plist file data operations.

Tools/Materials
    • MAC OS X operating system
    • Xcode compiler
Method/Step
  1. Create a. plist file.

    Method:

    1. Select Xcode,file-new-file ... ;

    2, in the project directory right-click, New File ... ;

    3, select Xcode, use shortcut cmd+n key combination;

    4, Code creation (next step).

    Effect. You can select the "property list" in any of the "Resource" in iOS and OS X to create and name the save. The current name is: Userslist.plist.

  2. Add data to the. plist file and display

    Click Userslist.plist in the Xcode project to open the content that appears in the Userslist.plist file.

    You can add data by right-clicking on the "Add row" in the blanks or "root" line in the point. The General data type (type) can be selected for types such as array, Dictionary, string, and so on. You can also assign values (value) to the added data.

    In addition to being displayed as a list of property lists, the Userslist.plist file is selected, and the right-click "Open as"-"Source Code" can be displayed in XML format. The current operation result.

  3.  

    creates a. plist file using code.

    //Get local sandbox path

        Nsarray *path = Nssearchpathfordirectoriesindomains ( NSDocumentDirectory, Nsuserdomainmask, YES);

       //Get full path

        NSString *documentspath = [path objectatindex:0];

        NSString *plistpath = [Documentspath stringbyappendingpathcomponent:@ "Userslist.plist"];

        Nsmutabledictionary *usersdic = [[Nsmutabledictionary alloc] init];

       /Set property values

        [usersdic setobject:@ "Chan" forkey:@ "name"];

        [usersdic setobject:@ "123456" forkey:@ "password"];

       //write file

        [Usersdic Writetofile:plistpath Atomically:yes];

  4.  

    add modify data action

          Add or modify existing data. This file is not an existing Userslist.plist file, but a file that exists inside the sandbox. The userslist.plist content is unchanged. Need to view can be viewed into the simulator sandbox.

    //Get an existing full path

        NSString *plistpath = [[NSBundle mainbundle] pathforresource:@ "Userslist" OfType : @ "plist"];

        Nsmutabledictionary *usersdic = [[Nsmutabledictionary Alloc]initwithcontentsoffile:plistpath];

       //Set property values, no data is new, existing data is modified.

        [[Usersdic objectforkey:@ "users"] setobject:@ "tease" forkey:@ "name"];

        [[Usersdic objectforkey:@ "users"] setobject:@ "dbdbdb" forkey:@ "password"];

       //write file

        [Usersdic Writetofile:plistpath Atomically:yes];

     

     

    If you want to use a different file name to create a new plist file from the path

    NSString *filename=[plistpath stringbyappendingpathcomponent:@ "Test.plist"];//already have the name file is not new, no new.

     

     

    If you want a multi-layered structure.

     

iOS Development plist file operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.