Detailed examples of loading plist Data Tables

Source: Internet
Author: User

Detailed examples of loading plist Data Tables

1. This document mainly loads plist data (App. plist) of a single dictionary in the following format:

 

 

2. Load the plist data and convert the data dictionary to the corresponding model. In this example, the icons of each application are used, create a model huapp with the app icon (name self-developed ). Define all variables in the header file and convert the dictionary into model data. For example:

 

 

Class method calls the object method and converts the model in the object Method

 

Note: You can use KVC to convert attributes in the object method. This can be solved with a single line of code.

[Self setvaluesforkeyswithdictionary: dict];

However, when KVC is used, all attributes in the dictionary are converted at one time, and an error is returned if unused attributes are missing.

 

3. In the controller, define an array to store the standby information, load the array with laziness, and convert the model data;

/Load application information

@ Property (nonatomic, strong) nsarray * Apps; // Save the array to the backup information

The conversion example code is as follows:

-(Nsarray *) apps

{

If (_ apps = nil)

{

// Obtain the full path of plist

Nsbundle * Bunder = [nsbundle mainbundle];

Nsstring * Path = [Bunder pathforresource: @ "app. plist" oftype: Nil];

// Load the Array

Nsarray * dictarray = [nsarray arraywithcontentsoffile: path];

// Convert the array dictionary into a model object and store it in a new array.

Nsmutablearray * apparray = [nsmutablearray array];

For (nsdictionary * dict in dictarray)

{

// Create a model object

Huapp * APP = [[huapp alloc] initwithdict: dict];

// Add the model object to the array

[Apparray addobject: app];

}

_ Apps = apparray;

}

// Return data

Return _ Apps;

}

4. When using data, app. Icon is the corresponding icon name, and App. Name is the corresponding application name.

First, retrieve the corresponding model: huapp * APP = self. app [I]; // I is the corresponding location

Uibutton * button = [[uibutton alloc] init];

[Buttonset image: [uiimageimage withnamed: icon] forstate: uicontrolstatenormal]

[Button settitle: App. Name forstate: uicontrolstatenormal];

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.