Object-C code specifications

Source: Internet
Author: User

Point references are authentic objective-C2.0 style. It is used for simple set and get operations, but other actions of the object are not
It should be used.


Copy nsstring in setters
Never retain a single string. This prevents the caller from modifying the string without your knowledge. Do not make the assumption that the object you accept is an nsstring object rather than an nsmutablestring object.

-(Void) setfoo :( nsstring *) afoo {
[Foo _ autorelease];
Foo _ = [afoo copy];

} Conclusion: the setter that accepts nsstring as the parameter should copy the string accepted by nsstring.

Logically, the setter of nsstring must use copy instead of retain.


Classes that implement the delegate mode should:

Has a member variable named delegate _ to reference the delegate.
Therefore, the accessors should be named delegate and setdelegate :.
The delegate _ OBJECT should not be retained.

Do not directly compare bool variables with Yes/No. Not only does this affect readability, but the results may be different from what you think.

Use nil check to check the applicationProgramInstead of avoiding crashes. The objective-C Runtime will process the object to an Nil
Message sending status. If the method does not return a value, it does not matter.


Summary: Do not throw an objective-C exception, but you are prepared to capture the exception from a third-party call or system call.


When objective-C ++ is used to write stack-based objectsCodeIf an objective-C exception is thrown, the object will not be cleared.
. (Destructor)


Objects in dealloc should be released in the same order as they are declared in @ interface.


A u to lea se after RETA in

When assigning a new object to a variable, you must first release the old object to avoid Memory leakage. There are some correct methods for processing
It. We chose the retain method after autorelease because it turns out that it has fewer errors. Note that large cycles will fill up
Autorelease pool may be less efficient, but this compromise is acceptable.

-(Void) setfoo :( gmfoo *) afoo {
[Foo _ autorelease]; // won't dealloc if | Foo _ |=| afoo |
Foo _ = [afoo retain];

}

Conclusion: when assigning values to objects, the retain mode after autorelease is followed.


When creating a temporary object, use autolease In the same line, instead of using a single
Independent release.

Do not call the nsobject class method new or override it in the subclass. Instead, you should use the alloc and init Methods
Create and initialize an object.



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.