IOS skills, features, and specifications

Source: Internet
Author: User

IOS skills, features, and specifications

I. compiler features

1. ARC.

ARC is a compiler feature. If ARC is used in the project, the compiler automatically adds the OC object counter release code during project compilation. If ARC is used, the release, retain, retainCount, and [super dealloc] codes are not allowed in the project.

Unlike garbage collection in Java and. Net, garbage collection in Java and. Net is a runtime feature. ARC only automatically reduces the counter of the OC object by 1. Non-OC objects, such as common data types, enum enumeration, and struct are not involved in the ARC. The system will automatically destroy and recycle them. (Note: The OC object counter occupies 4 bytes in an object. When the counter is 0, if the OC object is referenced by a strong pointer, the OC object will become a zombie object and cannot be accessed again. Of course, the object cannot be "Revived" and the strong pointer should be set to nil manually; if the OC object is referenced by the weak pointer, when the OC object becomes a zombie object, the weak pointer will be automatically destroyed .)

2. @ class

@ Class appears in the class header file. @ Class mainly solves two problems:

① In non-ARC projects, the problem that the OC object counter cannot be reduced to 0 due to mutual reference between classes is solved. Reference the header file of another class in one class header file to use @ class, and set all OC object pointers of one class to assign type.

② Improve compiler features. Using @ class is equivalent to a declaration that tells the other class which member variables and methods are contained. When using. m, you only need to # import the. h file of this class. If you do not use @ class to declare class references, all of them are in. # import another class directly in the H file. h file, then when. when the H file is changed, the compiler will copy it again during compilation. h header file, with tens of thousands of such applications added, the compiler has to copy 10 thousand times. You can use @ class without copying, which improves the compiler performance.

3. []

For NSArray and NSDictionary, if you need to directly obtain the corresponding value based on the index or keyword key, you can obtain the value directly through [index] or [key. Note: all the objects obtained through this method are of the id type. To access the methods or member variables of the specified object type, you also need to go through them. (Note: Why is the object obtained through [] an id type? Because [index] is equivalent to calling the objectAtIndex method of the NSArray object, and [key] is equivalent to calling the objectForKey. Both methods return id-type objects .)

4. @ () and @ 20

@ () And @ 20 are used when the int integer is converted to the NSNumber of the OC object. For example, @ 20 refers to converting integer 20 to the OC object type of NSNumber, which can be used in NSArray or NSDictionary. (Note: NSArray and NSDictionary can only add OC objects as elements .). @ () Is to convert common temporary variables to the specified OC type. For example, int a = 20; NSNumber * aa = @ (a); then, the integer value of the Temporary Variable a is converted to the OC type of the NSNumber. You cannot write @ a in this way. An error is returned when you write it.

 

Ii. Performance

1. SEL

SEL is the Selector. SEL is the pointer type pointing to methods in the class. Generally, when an object or class calls an object method or class method, you must first package the called method into the SEL type, find the address of the method called in the corresponding class based on the SEL type data, and finally call the method. Such an object or class calls a specified method goes through two steps. If multiple frequent calls to the same method are involved or each call must go through SEL, it will undoubtedly consume a certain amount of performance. In this case, you can save SEL separately. Each call can directly find the corresponding method based on the SEL type pointer address for execution.

2. nonatomic

When a Class header file declares a member variable, the system declares this variable as atomic by default. atonmic indicates the atom and is used in multiple threads, it prevents deadlocks caused by simultaneous access of the same Member or object by multiple threads. The default value is atomic. when the project is compiled, the system automatically adds a lot of code to the background. multithreading is not involved in normal times. You can manually set it to nonatomic to improve performance.

3. [UIImage imageNamed:]

A UIImageView uses this method to access and display an image. The system automatically generates a cache for this image in the memory. If this cache is not cleared from the memory, it will continue to occupy the memory. If you use imageWithContentsOfFile, when UIImageView is displayed as another image from one image, the system will automatically remove the previous image from the memory.

4. Custom UITableViewCellIf an image only controls its hidden attribute, you can set the image of UIImageView to this image during UITableViewCell initialization, avoid memory consumption when UITableViewCell repeatedly sets images from the cache pool.

5. The height of custom UITableViewCell is inconsistent.You need to introduce the frame model to calculate the frame of each UITableViewCell immediately after obtaining the data. You can directly set the calculated height when using it. This avoids freezing caused by duplicate computing consumption performance when UITableView is rolled up or down.

 

Iii. Skills

1. A line is displayed on the interface.In WPF, you can use the Border or Line ready-made controls. In OC, Apple officially uses UIView. If it is a horizontal line, the height of the UIView is 1, and in the case of a vertical line, with is 1.

2. the left and right sides of UITextField are leftView and rightView.. If you want to automatically leave a gap on the left when entering the value, you can use the specified leftView as [[UIView alloc] initWithRect: CGRectMake (0, 0, 8, 0)];

3. Set an icon for a UIButtonAnd the icon cannot be scaled or displayed. You can set the UIButton image to display the icon instead of the BackgroundImage.

4. image tile technology.For example, in the QQ chat window, messages are sent by each user. In fact, the Program specifies the number of upper, lower, left, and right ranges of an image. When the image is stretched, It is not deformed, and the area in the range is automatically tiled. In Win8, this technology is called NineGrid (nine slices ).

 

Iv. debugging

1. Keyword of the error message:ForUndefinedKey. At first glance, we can see that the line in the StoryBoard is wrong.

2. Unrecoganized seletor sent to instance.First, we can find the object or class method.

There are two common causes for this situation:

①. The subclass calls the custom constructor of the parent class for initialization. The parent class method does not use self, and the returned result is the parent class object. The subclass method is called.

② When defining object methods, the return value uses the id type instead of instancetype. As mentioned above, the id type cannot be automatically converted to a specified object, and any type of objects are returned by default. For example, if a Person object is to be returned and no length member is defined, in this case, the NSString type pointer can be used to point to this object, and the length member accessing NSString will see this error. To solve this problem, you can convert it to the Person object or change the returned type id to instancetype. Is it possible to change the instancetype in all regions where IDs are used? No. It can be replaced with instancetype only when id is the return type.

3. has been modified.You can clear the project cache at a glance.

 

5. Layout

We can try to remove autolayout when we do not see the changes when modifying the interface running.

 

Vi.. pch File

1. Try to put our own defined items in @ ifdef _ OBJC.

For example, import a. h header file in the. pch file, # import "Person. h ". It is not written in @ ifdef _ OBJC _. At this time, we write a. c file in the project, and many errors will be reported when it is run. Why? Because in. pch import # import. h file, which will be imported by default in all files of the Project # import this. h header file, but the C language. the c program requires # include before it can be imported. h header file. If # import is used, an error is returned.

2. You must use a custom log printing policy,Otherwise, the project will contain a lot of log printing code at the time of release, which will undoubtedly affect the performance. The custom log printing policy is as follows:

So, read a project code and first go to the. pch file.

 

VII. Specifications

Both the class name and function name are in upper case, the first letter of the method name and variable name is in lower case, and the first letter of other words is in upper case.

 

8. Understanding

1. Why does UITableViewCell need Identifier, while the custom View of UIDatePick does not.

UITableViewCell Identifier mainly takes into account the following two points:

① Different cell templates are used for different data, and the specified cell template can be recycled from the cache pool according to different Identifier.

② Remove existing templates from the cache pool based on Identifier and reuse them cyclically to improve performance.

The View of UIDatePick is the same template and does not have different templates for the same data source. Therefore, you do not need to use the resuming directly by using Identifier.

 

Pay attention to the usage of awakeFromNib, layoutSubView, and didMoveToSuperView.

 

IOS is still learning, and there are some places that cannot be understood or are wrong. Please give your best advice.

Related Article

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.