52 Essentials of iOS Development summary of the Five

Source: Internet
Author: User

21. Understanding OBJECTIVE-C Error Model

@throw [nsexception exceptionwithname:nsinternalinconsistencyexception reason:@ "must be Overriden" UserInfo:nil] Exceptions are only used to handle critical errors.

In the case of a less serious error, the programming paradigm of the OC language is: To return the method to nil/0, or to use Nserror to indicate that an error occurred.

-(ID) Initwithvalue: (ID) value{

if (self = [super init]) {

If (/*value means instance can ' t be created*/) {

self = nil;

}else{

Initialize instance

}

}

return self;

}

The usage of nserror is more flexible and can be rewarded to the caller for the wrong reasons. The Nserror object encapsulates three messages:

Error domain (fault range, whose type is string)

Error code (fault code, whose type is an integer)

User info, which is a dictionary type

The first common use of Nserror when designing an API is to pass an error through a delegate protocol.

As nsurlconnection in its delegate agreement Nsurlconnectiondelegate, the following methods are defined:

-(void) connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) error

Nserror another usage is to return to the caller via the method's "output parameter", such as:

-(BOOL) dosomething: (Nserror *) error

You should use exceptions only if there are serious errors that can cause the entire application to crash.

In cases where the error is less severe, you can assign a delegate method to handle the error. Or as an output parameter.

22. Understanding the Nscopying Agreement

If you want to make a copy of the object you write, you need to implement the Nscopying protocol and implement the prescribed method:

-(ID) Copywithzone: (Nszone *) zone;

If a custom object is divided into a mutable version and an immutable version, then both the nscopying and nsmutablecopying protocols are implemented.

When copying objects, you should decide whether to use a shallow copy or a deep copy, in general you should do a shallow copy as much as possible.

If you are writing objects that require a deep copy, consider adding a new method that specifically performs deep copies.

23. Inter-data communication through delegation and data source protocol

The delegate mode provides a set of interfaces for an object so that it can communicate related events to other objects.

Define the interfaces that the delegate object should support as protocols, and define the events that may need to be handled as methods in the protocol.

When an object needs to be from another object or data, the delegate mode can be used, which can be referred to as the "Data Source Protocol (protocol)" in this mode.

If necessary, a struct containing a bit segment can be implemented to cache the information that the delegate object responds to the relevant protocol method.

24. Spread the implementation code of the class into several categories that are easy to manage

Use the classification mechanism to divide the implementation code of the class into manageable chunks.

The methods that should be considered "private" are grouped into categories called Private to hide the implementation details.

25. Prefix the category name of the third party class

When you add a taxonomy to a third-party class, you should always prefix the name with your dedicated one.

When you add a taxonomy to a third-party class, you should always include the method name with your dedicated prefix.

52 Essentials of iOS Development summary of the Five

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.