[Technical translation] building a modern Objective-C (I)

Source: Internet
Author: User

When learning a new skill, such as a programming language, we often combine all the usable ones in order to be able to run. Later, we went back to these habits and re-estimated them to use the best practices in the community and write better and more structured code.

Recently, Objective-C has received many new features, but the Community's best practices have not been updated. This is beyond the scope of "style" and enters the field of "structure.

Recently, I reviewed my own code practices and evaluated where I could do better. So I think I should share my findings with you.

Welcome to modern Objective-C.

(For the above part, thanks to the translation by Ley)

Instance Variable)

Alas, instance variables. Where can I start. In a word, the instance variable is very bad. If you write:

 

Don't write it like this. Change it now.

Do not declare instance variables any more,EspeciallyDo not declare it in the header file. You should declare themPropertyAnd then use the message mechanism or "."AccessThey.

I have said before why there is no obvious benefit in accessing property through instance variables. In fact, there are several advantages to access through the getter/setter method.

  • Consistency: you do not need to doubt whether the getter/setter method has any side effects. If yes, it will be noticed for a long time.

  • Debug: You can easily set a breakpoint on the getter/setter method, instead of setting a watchpoint for the memory address of the instance variable at run time.

Really, there is no reason to declare the instance variables and directly access the attributes in the form of these instance variables-unless it is in its own override getter/setter method, or in the initializer/dealloc method, it depends on how defensive the code you want (thanks to Bryan for providing the link ). Unless you get used to it, you should get rid of it. I changed it.

Update: I found a link to the official document.NoCall the getter/setter method in dealloc. For reference.

So what about read-only attributes? Since there is no setter method, do you still need to access instance variables? Good question. This introduces the next point in this article.

Define the readonly attribute in the header file

To expose attributes or components in your public interface, using the readonly attribute is a good method. But if you do not directly access the instance variables, how do you set their values? The answer is to define a private class extension in the. m file.

In your header file, the statement is as follows:

 

Then, in the implementation file of the classBased onAnd the definition is as follows:

 

Then you definePublicGetter andPrivate. Congratulations! Now you no longer need to access instance variables.

Schwa adds a suggestion:

@ Ashfurrow: Define the readonly attribute in the header file. I want to add a variable object instead of exposing the mutable object in the header file. A good article, by the way.

-Jonathan Wight (@ schwa) January 24,201 4
Reasonably define BOOL type attributes

When defining attributes, follow the Apple official guide. But I admit that I am not always so diligent. Remember to manually define a getter method when defining attributes of the BOOL type.

@property (nonatomic, assign, getter = isSomething) BOOL something;
If not necessary, do not write # import in the header file.

I often see this in Objective-C's new handwritten code. In general, the problem is that most # import statements should only be written in the. m file, rather than in the. h header file.

The following is an example.

 

You can rewrite the Code as follows, and then import the # import mytherclass. h header file in the implementation file of the class yourMyClass. m.

 

@ Class mytherclass is written as follows:ClassForward Declaration (Forward class declaration).

This writing has many benefits. Replacing the # import header file with the class Forward Declaration can improve compilation speed, avoid loop # import, and make your header file lighter-this should be the case.

(To be continued)

Structuring Modern Objective-C Translator: Ley, Dai cangshu

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.