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];