A summary of knowledge points of dictionary to model

Source: Internet
Author: User

Because of the basic, the dictionary to the model of many details of the knowledge point is also known, so the study of the dictionary to the model of the relevant issues are summarized, which I do not quite understand. Parsing from loading to individual statements may

A place of discrepancy.

Knowledge points to be mastered when translating a dictionary into a model, specific examples

app Loading Process

1> app loads from Mainbundle plist

2> determine the size and position of each view according to the number of data in plist

3> However, many icons like this, the control of a lot of UI design, it is recommended not to use the storyboard, but the use of code to create, or later maintenance is also troublesome.

When using a dictionary, to put the supporting files into the plist file, and drag the footage to the supporting files, you need to be aware of the selection of items checked when dragging:

In most cases, when dragging footage into a project, you usually choose Destination,

Folders: Select the first item: Create a group, so that the Xcode navigation explicit is the yellow folder, you know, Xcode resource material is a sub-folder storage, but in the bundle all the material is in the same folder, so tick the development efficiency is very high, but, Can not appear the file name of the situation, will make the art uncomfortable. Features: can be directly used [NSBundle Mainbundle] as a resource path, high efficiency! You can load an image using [UIImage imagenamed:]

If you select the second item, create a reference to the file home: Xcode appears to be a blue folder, at this time the resources in Xcode sub-folder, in the bundle also sub-folder, therefore, can appear the file name of the case, features: Need in [NSBundle Mainbundle] On the basis of stitching the actual path, the efficiency is poor! Cannot load image using [UIImage imagenamed:]

NSBundle the understanding and Mainbundle class method NSBundle Mainbundle

Function: Produces the full path of the associated file.

Bundle is a directory, which contains the resources used by the program, such as sound images, in order to facilitate the extraction of these files, the corresponding Bundle,cocoa provides NSBundle, which inherits from NSObject.

We put all the resources in a directory, is the main bundle, so we must first get the main bundle (home directory) through NSBundle, in the main directory to find the corresponding named sub-resources. When we get a NSBundle object, we have access to the resources.

NSBundle *bundle = [NSBundle Mainbundle]

NSString *path = [Bundle pathforresource:@ "App.plist" oftype:nil];

Or:

NSString *file = [[NSBundle mainbundle] Pathforresource:name Oftype:nil];

userinterfaceenable user interaction behavior, when set to Yes, is able to interact with the user and not when set to No. For example UIView and Uiimageview, generally uiview in principle Uiimageview is a subclass of UIView, then according to the Richter replacement principle, any uiview place should be able to use a uiimageview substitution. So I used a uiimageview to replace the view in the Mainviewcontroller to make the background image. However, the problem comes out, above put a uitableview (Addsubview), this uitableview can only display data, but can not scroll, if I imageview and TableView are addsubview up then everything is normal, This is because UIView's userinterfaceenable is yes, able to interact with the user, to handle scrolling when you swipe the screen, but Uiimageview's userinterfaceenable no response when you touch the screen. That is, there is no way to interact with you.

Uiimageview and UIImage difference

1.UIImageView internal picture tiling (tile) not supported

2. The images in the resource are lowercase, and the simulator may not be case-sensitive, but it can be differentiated in the real machine.

[UIImage imagenamed:@ ""]; Case sensitivity in devices

3.UIView has no background map properties and background color properties. Setting the background map can be addsubview (backgroundimage);

4.[uiimage imagenamed:@ ""];//It's a cache feature.

This method looks in the system caches for a image object with the specified name and returns that object if it exists. If a matching Image object is not already in the cache, this method loads the image data from the specified file, caches I T, and then returns the resulting object.

On a device running IOS 4 or later, the behavior is identical if the device ' s screens have a scale of1.0. If the screen had a scale of2.0, this method first searches for an image file with the same filename with [email protected ] appended to it. For example, if the file ' s name Isbutton, it first searches [email protected] If it finds a 2x, it loads that image and SE TS Thescale property of the Returneduiimage object to2.0. Otherwise, it loads the unmodified filename and sets Thescale property to1.0.

On IOS 4 and later, the name of the file was not required to specify the filename extension. Prior to IOS 4, you must specify the filename extension.

It is possible to apply a memory warning that often occurs after multiple operations to cause an automatic exit problem. After locating, it was found that the image assigned by [UIImage imagenamed: @ "] was not released. The information previously obtained from the official reference should be [UIImage imagenamed:@ "] and the assigned image system will be placed in the cache. There is no explicit introduction to the cache management rules. From this it appears [UIImage imagenamed:] Only suitable for reading the map with small size of the UI interface, while some larger resource files should try to avoid using this interface.

5. + (UIImage *) Imagewithcontentsoffile: (NSString *) path//This method does not cache the image object.

ReadOnly in OC, when you use ReadOnly in @property only means that the compiler will automatically generate getter methods for you, and will not automatically generate setter methods for you. Now that the compiler doesn't give you the setter, you don't define the setter method yourself. Then of course you can't assign a value. So if you want to change the attributes, you need to rewrite the set method to change the properties by means of the method.

9 analysis of algorithm between Gongge and column number

1, load a uiview, each gongge corresponding to a uiview, on the basis of UIView and then load the chart (Uiimageview) and uilable and "Download" UIbutton.

2, calculate the position of each uiview (Cgfolat x and Y),

UIView *appview = [[UIView alloc] Initwithframe:cgrectmake (x, Y, Vieww, VIEWH)];

3. Add UIView details (uiimageview) and uilable and "Download" UIbutton.

4. Handle the "Download" UIButton Click event.

Problems involved in dictionary-to-model:

1, class understanding, dictionary to model, equivalent to the constant generation of model (class) objects, and objects have properties in the class, these property values are their corresponding value in the dictionary.

2, now convert plist into a dictionary array, put the dictionary in the array, and then the array into the model array, the array is a lot of objects.

The code involved in each function is:

In the viewcontroller.m file, call the encapsulated dictionary-to-model method

-(Nsarray *) questions{    if (!  _questions) {        = [lfquestion questions];    }     return _questions;}

Set the corresponding attribute value in the dictionary in the model. h file and the method declaration required, a class method to instantiate the object, and a member method for the Dictionary instantiation object

#import<Foundation/Foundation.h>@interfaceLfquestion:nsobject@property (nonatomic, copy) NSString*answer; @property (nonatomic, copy) NSString*title: @property (nonatomic, copy) NSString*icon; @property (nonatomic, strong) Nsarray*options; @property (Nonatomic, strong) UIImage*image;/** Use dictionary to instantiate the member method of an object*/-(Instancetype) Initwithdict: (Nsdictionary *) dict;/** class method for instantiating objects in a dictionary, also known as factory methods*/+ (Instancetype) questionwithdict: (Nsdictionary *) dict;/** Load an array of objects from Plist*/+ (Nsarray *) questions;@endmodel. M Method#import "LFQuestion.h"@implementationlfquestion+ (Instancetype) questionwithdict: (Nsdictionary *) dict{return[Self alloc] initwithdict:dict];}-(Instancetype) Initwithdict: (Nsdictionary *) dict{ Self=[Super Init]; if(self) {self.answer= dict[@"Answer"]; Self.icon= dict[@"icon"]; Self.title= dict[@"title"]; Self.options= dict[@"Options"];    [Self setvaluesforkeyswithdictionary:dict]; }    returnSelf ;}+ (Nsarray *) questions{Nsarray*array = [Nsarray arraywithcontentsoffile:[[nsbundle mainbundle] Pathforresource:@"questions.plist"Oftype:nil]]; //converting a dictionary into a dictionary arrayNsmutablearray *arraym =[Nsmutablearray array];  for(Nsdictionary *dictinchArray)    {[Arraym addobject:[lfquestion questionwithdict:dict]]; }//calls that involve class methods        returnArraym;}@end

A summary of knowledge points of dictionary to model

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.