IOS-Effective OBJECTIVE-C Read notes (7)

Source: Internet
Author: User

1: Implementing the Description method

NSlog when exporting a custom class, only the class name and the memory address of the object are exported. It is also easy to output more useful information, just overwrite the description method and return the string that describes the object.

-(nsstring*) description{

return xxxxxxx;

}

There is an easy way to output a lot of different information in description, that is, with the help of the description method of the Nsdictionary class.

-(NSString *) description{

return [NSString stringwithformat:@ "%@", @{@ "title: _title, @" age ": @ (_age)"} ";

}

There is also a method in the NSObject protocol debugdescription, the intent of this method is very similar to description, and the difference is that the Debugdescription method is called when the developer prints the object in the debugger with a console command. In the default implementation of NSObject, this method simply calls the description

Using the method, we insert a breakpoint after the code used to create the instance, and then pass the debugger to pause it, and when the program runs to the breakpoint, the developer can enter commands into the debugger console.

' po ' command, you can complete the object printing (prient-object) work.

Also, you can override the Debugdescription method to make it more your idea.

Summarize:

Implementing the Description method returns a meaningful string that describes the instance

If you want to print a more detailed description of the object during debugging, you should implement the Debugdescription method, the ' po ' command, the Print object

2: Use immutable objects as much as possible

When designing a class, you should use attributes to encapsulate the data. When using properties, you can use their life as ' Read Only '. By default, properties are ' readable and writable ', so that the designed classes are ' mutable '. However, in general, the data we want to model does not necessarily need to change.

As far as programming practice is, it is necessary to make the externally published properties read-only, and to publish them when they are needed.

Summarize:

Try to create immutable objects

3: Use a clear and coordinated naming method

Try to use the ' camel-case naming ' method

Method name length should be moderate, clear expression, and concise.

Considerations for naming methods

(1) If the return value of a method is newly created, the first word of the method name should be the type of the return value, unless there is a modifier in front of it. The access method for a property does not follow this naming scheme. The access method for a property should be named according to its corresponding property.

(2) The noun representing the parameter type should be placed before the parameter.

(3) If the method is to perform an operation on the current object. Then you should include verbs, and if you do, you'll need parameters. You should add one or more nouns after the verb.

(4) Do not use short names such as STR, but should use a full name such as String.

(5) The Boolen attribute should be prefixed with the IS, and if a method returns a Boolean value that is not a property, then the has or is current prefix should be selected according to its function.

(5) Leave the get prefix to those methods that borrow the ' output parameter ' to hold the return value.

Naming of classes and protocols

The name of the class and protocol should be prefixed to avoid namespace collisions. And it should be as good as the name of the method to organize the words, so that they read from left to right more fluent.

The naming conventions for classes and protocols should be consistent.

Summarize:

Names should be followed from the standard OC naming specification

The method name should be concise and read smoothly from left to right.

Do not use the abbreviated type name in the method name

Naming the specification for private methods

Name private methods starting with ' P_ ', so it's easy to differentiate

Do not but use an underscore ' _ ' to prefix the private method, because this method is reserved for Apple.

IOS-Effective OBJECTIVE-C Read notes (7)

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.