Objective-C 2.0 Study Notes (3)

Source: Internet
Author: User

The following are the notes for learning objective-C 2.0. The original book I purchased is the original English version. Due to my limited level of English, there may be omissions in understanding.

Original book purchase address: Amazon


3. Interface and API design


Item 15: Use prefix names to avoid namespace clashes

Some knowledge points:

(1) because Apple's system framework names all start with two letters, in order to avoid conflicts with them, developers Define a naming prefix that should start with at least three letters.

(2) because the third-party library prefix name is modified, be sure not to reference the third-party library repeatedly.

Remember:

(1) Select a prefix name suitable for your company, application, or both, and strictly name the prefix.

(2) If you use a third-party library as an independent reference, consider adding your own prefix name to it.


Item 16: have a designated initializer

Remember:

(1) implement a custom initial function in the class you write, and provide instructions on which function is used. Other initial functions must be called through this custom initial function.

(2) If the customized initial function is different from the parent class, make sure that the initial function of the parent class is overwritten in the subclass.

(3) The initial function of the overwritten parent class needs to throw an exception to avoid being called in the subclass.


Item 17: implement the description method

Some knowledge points:

(1) The description method is called when the nslog object is executed. The debugdescription method is called only when the Po command is executed during debugging.

Remember:

(1) Implement the description method and provide a meaningful string in the description function.

(2) If the object needs to display more detailed information during debugging, You can implement the debugdescription method.


Item 18: prefer immutable objects

Some knowledge points:

(1) When a property is set to readonly, you do not need to set write attributes such as copy and assign.

Remember:

(1) try to create objects that cannot be changed.

(2) If you need to set the attributes internally, you can set readwrite in the class continuation category and set readonly in the interface header file.

(3) It is better to provide a read/write Method for a property than to directly set the changeable property in the property.


Item 19: Use clear and consistent naming

Some knowledge points:

(1) Some Suggestions on method naming:

A. If a newly created value is returned by a method, the first word of the method name should be the type of the value unless necessary limitation words need to be added before the type.

B. A noun describing the parameter type must appear before the parameter.

C. If a method contains an operation on an object, you need to add a verb indicating behavior after the noun before the parameter to specify the operation.

D. Do not use abbreviations in method names. For example, use STR instead of string.

E. Add is before the bool type attribute method. If a method returns a bool type, add an has or is as needed.

F. Add get before the method name for obtaining the value.

(2) Suggestions on naming classes and protocols:

A. Add the necessary namespace before the custom class.

B. Integrate a class. Note that the last word needs to clarify the type of the parent class.

C. The delegate name must end with the delegate word.

Remember:

(1) follow the basic objective-C interface name to extend the name of your methods and classes.

(2) Make sure that the method name is concise but clear and can be read from left to right just like a sentence.

(3) abbreviations are not allowed.

(4) The most important thing is to make sure that your naming method is consistent or that it is consistent with the module to be grouped.


Item 20: prefix private method names

Some knowledge points:

(1) You can consider using P _ as the private method prefix.

Remember:

(1) Add a special prefix to a private method to distinguish it from a public method.

(2) do not use a separate underline as the method prefix, because Apple's own method uses this rule to avoid rewriting Apple's private method.


Item 21: Understand the objective-C Error Model

Some knowledge points:

(1) Use the-fobjc-arc-exceptions flag to use the arc with exceptional security. However, it is still not recommended to use exceptions in objective-C. Exceptions may cause memory leakage to reduce program performance and crash.

(2) One of the usefulness of exceptions in objective-C is to be used in some abstract class methods that need to be inherited. If these methods that must be inherited and overwritten are not overwritten, an exception is thrown.

(3) exceptions are only used for some fatal errors. For non-fatal errors, nil/0 is usually returned or nserror is returned.

(4) nserror generally contains three content:

A. Error domain (string) defines the domain where an error occurs.

B. Error Code (integer) is globally unique and defines an error domain identifier. It is usually an enum.

C. User Info (dictionary) contains additional information about the error or other information about the error.

(5) An object transmits an nserror over the proxy protocol.

(6) another way to transmit nserror is to use the parameter method. Use a pointer to the pointer as the nserror parameter type. If you do not care about the details of the error, you can return a Boolean value as a flag for judging the successful call failure.

(7) When nserror is returned as a parameter to identify an error, you must first determine whether the returned nserror is null and then read the specific value of nserror.

Remember:

(1) The exception method is considered only when a fatal error is obtained and the program crashes.

(2) For non-fatal errors, use the proxy method to transmit nserror or the External Parameter Method to transmit nserror.


Item 22: Understand the nscopying Protocol

Remember:

(1) If the created object needs to be copied, The nscopying protocol must be implemented.

(2) If the object you create contains both changeable and unchangeable variables, you must implement both nscopying and nsmutablecopying protocols.

(3) determine whether a copy is a deep copy or a light copy. If possible, it is more inclined to implement a light copy.

(4) If your object requires deep copy, consider adding a method for deep copy.

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.