Summary of effective methods for writing high-quality iOS code and OS X code
Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="background-color: #ff00ff;">first, familiar with Oc:</span></p></p><p><p><span style="background-color: #ffffff; color: #ff00ff;">Learn about the origins of Oc:</span></p></p><p><p><span style="background-color: #ffffff; color: #ff00ff;">The object-oriented languages such as OC and C++,java are similar, though there are many Differences. Because the language uses a message structure rather than a function call.</span></p></p><p><p><span style="background-color: #ffffff; color: #ff00ff;">The difference between a message structure and a function call is that the code that is to be executed at its runtime is determined by the runtime environment, which is determined by the compiler,</span></p></p><p><p><span style="background-color: #ffffff; color: #ff00ff;">If the function call is polymorphic, the "virtual method table" (that is, The mechanism used for dynamic dispatch and runtime method Binding) detects exactly which function is being implemented.</span></p></p><p><p><span style="background-color: #000000; color: #ff00ff;">About the forward statement</span></p></p><p><p><span style="background-color: #ffffff; color: #ff00ff;">1. Do not introduce a header file unless it is indeed necessary. In general, you should use the forward description in the header file of a class to refer to another class,</span></p></p><p><p><span style="background-color: #ffffff; color: #ff00ff;">and introduce the header files of those classes in the implementation file. Doing so minimizes the coupling between classes (coupling).</span></p></p><p><p><span style="background-color: #ffffff; color: #ff00ff;">2. It is sometimes impossible to use forward declarations, such as declaring a class to follow an Agreement. In this case, try to keep that class in line with some of the declarations</span></p></p><p><p><span style="background-color: #ffffff; color: #ff00ff;">Move to the class-continuation category and, if not, Place the protocol in a single header file and introduce it.</span></p></p><p><p><span style="background-color: #ffffff; color: #ff00ff;">(if a protocol needs to be used in more than one class, you can create a new. h file so that the public protocols are written in it and can be used in many places)</span></p></p><p><p><span style="background-color: #ff00ff; color: #000000;">About literal syntax</span></p></p><p><p><span style="background-color: #ffffff; color: #ff00ff;">1 The general method of using literal syntax instead of such an object creates Nsstirng,nsnumber,nsarray,nsdictionary.</span></p></p><p><p><span style="background-color: #ffffff; color: #ff00ff;">2 Accessing the elements of an array or dictionary by removing the label operation</span></p></p><p><p><span style="background-color: #ffffff; color: #ff00ff;">3 when creating an array or dictionary using literal syntax, if there is nil in the value, an exception is thrown, so make sure that the value does not contain nil</span></p></p><p><p><span style="background-color: #ffffff; color: #ff00ff;">4 Multi-Use type constants, Less # define preprocessing directives</span></p></p><p><p><span style="background-color: #ffffff; color: #ff00ff;">Such as: defined in The. m file</span></p></p><p><p><span style="background-color: #ffffff; color: #ff00ff;">static const Nstimeinterval Kanimationduration = 0.3</span></p></p><p><p><span style="background-color: #ffffff; color: #ff00ff;">5 Global constants: use extern in the header file to declare global constants, define their values in their related implementation files,</span></p></p><p><p><span style="background-color: #ffffff; color: #ff00ff;">Such as: extren nsstring *const lxloginmanagernotification;. H</span></p></p><p><p><span style="background-color: #ffffff; color: #ff00ff;">NSString *const lxloginmanagernotification = @ "lxloginmanagernotification";. M</span></p></p><p><p><span style="background-color: #000000; color: #ff00ff;">About Enumerations: Ns_enum or ns_options</span></p></p><p><p><span style="background-color: #ffffff; color: #ff00ff;">1 using enumerations to indicate status, options, status codes</span></p></p><p><p><span style="background-color: #ffffff; color: #ff00ff;">Such as:</span></p></p><p><p><span style="background-color: #ffffff; color: #ff00ff;">typedef ns_enum (nsinteger, Dmtotalinvestmenttype) {<br>Dmtotalfincancetype = 0,<br>Dmholdfincancetype = 1,<br>Dmoutfincancetype = 2,<br>};</span></p></p><p><p><span style="background-color: #000000; color: #ffff00;">Ii. object, message, Operation period</span></p></p><p><p><span style="color: #ff0000; background-color: #000000;">About properties</span></p></p><p><p><span style="color: #ff0000; background-color: #ffffff;">1 You can use @property syntax to define the data that is encapsulated in an object</span></p></p><p><p><span style="color: #ff0000; background-color: #ffffff;">2 when reading data inside an object, it should be read directly through the instance variable, while writing the data should be written by Property.</span></p></p><p><p><span style="color: #ff0000; background-color: #ffffff;">3 in the initialization method and the Dealloc method, the data should always be read and written directly through the instance Variable.</span></p></p><p><p><span style="color: #ff0000; background-color: #ffffff;">4 sometimes using lazy initialization technology to configure a certain piece of data, this time through the property to read the data</span></p></p><p><p><span style="color: #ff0000; background-color: #ffffff;">Such as:-(UIView *) blueview{</span></p></p><p><p><span style="color: #ff0000; background-color: #ffffff;">If (!_blueview)</span></p></p><p><p><span style="color: #ff0000; background-color: #ffffff;">{_blueview = [blueview new];</span></p></p><p><p><span style="color: #ff0000; background-color: #ffffff;">}</span></p></p><p><p><span style="color: #ff0000; background-color: #ffffff;">Return _blueview;</span></p></p><p><p><span style="color: #ff0000; background-color: #ffffff;">}</span></p></p><p><p><span style="background-color: #000000; color: #ff0000;">About Object equivalence</span></p></p><p><p><span style="background-color: #ffffff; color: #ff0000;">1 detection of the equivalence of objects, providing isequal: and hash method</span></p></p><p><p><span style="background-color: #000000; color: #ff0000;">About class family patterns</span></p></p><p><p><span style="background-color: #ffffff; color: #ff0000;">1 To hide implementation details in class family mode, first define the abstract base class = = Refactoring</span></p></p><p><p><span style="background-color: #000000; color: #ff0000;"><span style="color: #ff0000;">Using association objects in existing classes</span></span></p></p><p><p><span style="background-color: #ffffff; color: #ff0000;">1 using associated objects to store custom data in existing classes</span></p></p><p><p><span style="background-color: #ffffff; color: #ff0000;">Such as:</span></p></p><p><p><span style="background-color: #ffffff; color: #ff0000;">#import <objc/runtime.h><br>static void *akey = @ "akey";<br>-(ibaction) doclick: (id) Sender {<br></span></p></p><p><span style="background-color: #ffffff; color: #ff0000;">Uialertview *alert = [[uialertview alloc]initwithtitle:@ "question" message:@ "what does the want to do?" delegate:self cancel buttontitle:@ "cancel" otherbuttontitles:@ "continue", nil];<br>void (^block) (nsinteger) = ^ (nsinteger Buttonindex) {<br><br>If (buttonindex==0) {<br>[self cancel];<br>}else{<br><br>[self docontinue];<br><br>}<br><br>};<br>Objc_setassociatedobject (alert, akey, block, objc_association_copy);<br>[alert show];<br>}<br>-(void) alertview: (uialertview *) alertview clickedbuttonatindex: (nsinteger) buttonindex{<br><br>void (^block) (nsinteger) = objc_getassociatedobject (alertview, akey);<br>Block (buttonindex);<br><br>}</span></p><p><p><span style="background-color: #000000; color: #ff0000;">Understand the role of objc_msgsend:</span></p></p><p><p><span style="background-color: #ffffff; color: #ff0000;">1 messages consist of the recipient, the selector and the Parameters. Sending a message to an object is equivalent to calling the method on the object being Changed. void Objc_msgsend (id Self,sel cmd,...) ;</span></p></p><p><p><span style="background-color: #ffffff; color: #ff0000;">2 All messages sent to an object are handled by the dynamic information distribution system, which detects the corresponding method and executes its code</span></p></p><p><p><span style="background-color: #000000; color: #ff0000;">Understanding Message Forwarding Mechanisms:</span></p></p><p><p><span style="background-color: #ffffff; color: #ff0000;">Frequently encountered crash Information: unrecognized selector sent to instance. this approach processes:</span></p></p><p><p><span style="background-color: #ffffff; color: #ff0000;">1 belongs to the class to see if you can dynamically add methods, dynamic method parsing;</span></p></p><p><p><span style="background-color: #ffffff; color: #ff0000;">2 The recipient is asked to see if there are other objects handling this information, if there is a transfer to it, if not to start the full message forwarding mechanism,</span></p></p><p><p><span style="background-color: #ffffff; color: #ff0000;">Give the recipient one last chance to try to resolve this message that is not currently being processed.</span></p></p><p><p><span style="background-color: #000000; color: #ff99cc;">third, Interface and API design</span></p></p><p><p><span style="background-color: #000000; color: #ff99cc;">Using prefixes to avoid namespace collisions</span></p></p><p><p><span style="background-color: #ffffff; color: #ff99cc;">1 Implement the namespace in disguise; choose a name that is associated with the company, app, or both as the class name Prefix.</span></p></p><p><p><span style="background-color: #ffffff; color: #ff99cc;">This prefix is used in all code, and if a third-party library is used in the library you are developing, prefix it with the name</span></p></p><p><p><span style="background-color: #ffffff; color: #ff99cc;">2 An All-in-one initialization method is provided in the class and declared in the document, and other initialization methods should call this Method.</span></p></p><p><p><span style="background-color: #ffffff; color: #ff99cc;">If the almighty initialization method and the superclass are different, the corresponding method in the replication superclass.</span></p></p><p><p><span style="background-color: #ffffff; color: #ff99cc;">3 Implement Description method to print meaningful information call Description method;</span></p></p><p><p><span style="background-color: #ffffff; color: #ff99cc;">If you want to print more detailed object description information during debugging, implement the Debugdescription method</span></p></p><p><p><span style="background-color: #ffffff; color: #ff99cc;">4 using a clear and coordinated naming method</span></p></p><p><p><span style="background-color: #ffffff; color: #ff99cc;">5 prefix the private method name</span></p></p><p><p><span style="background-color: #000000; color: #ff9900;">Iv. protocols and classifications</span></p></p><p><p><span style="background-color: #ffffff; color: #ff9900;">1 if you want to invoke an optional method on a delegate object, you must use the Type information Query method in Advance: respondstoselector</span></p></p><p><p><span style="background-color: #ffffff; color: #ff9900;">2 dispersing the implementation code of a class into manageable categories</span></p></p><p><p><span style="background-color: #ffffff; color: #ff9900;">3 do not define attributes in the taxonomy</span></p></p><p><p><span style="background-color: #ffffff; color: #ff9900;">4 Adding an instance variable to a class by classification, extending the private method is not known.</span></p></p><p><p><span style="background-color: #000000; color: #993300;">five, Memory Management</span></p></p><p><p><span style="background-color: #ffffff; color: #993300;">1 in the Dealloc method, the thing to do is to release the reference and dismiss the listener Notification.</span></p></p><p><p><span style="background-color: #ffffff; color: #993300;">2 Weak references avoid retaining rings:</span></p></p><p><p><span style="background-color: #ffffff; color: #993300;">3 Traversal Collection There are four ways: for loop, nsenumerator the calendar, fast through the calendar, block enumeration Method.</span></p></p><p><p><span style="background-color: #ffffff; color: #993300;">Multi-Use Block enumeration</span></p></p><p><p><span style="background-color: #ffffff; color: #993300;">Such as:-(void) enumerateobjectsusingblock: (void (^) (id object,nsinteger idx,bool *stop)) block;</span></p></p><p><p><span style="background-color: #ffffff; color: #993300;">-(void) enumerateobjectsusingblock: (void (^) (id object,nsinteger idx,bool *stop)) block{</span></p></p><p><p><span style="background-color: #ffffff; color: #993300;">If (SHOULDSTOP) {</span></p></p><p><p><span style="background-color: #ffffff; color: #993300;">Stop = yes;</span></p></p><p><p><span style="background-color: #ffffff; color: #993300;">}</span></p></p><p><p><span style="background-color: #ffffff; color: #993300;">}</span></p></p><p><p><span style="background-color: #000000; color: #008000;">six, block and Great Central distribution (GCD)</span></p></p><p><p><span style="background-color: #ffffff; color: #008000;">The concept of 1 blocks, can accept parameters and return values, can be allocated in the stack or heap, can also be global.</span></p></p><p><p><span style="background-color: #ffffff; color: #008000;">2 Creating a typedef for common block types</span></p></p><p><p></p></p><p><p>Summary of effective methods for writing high-quality iOS code and OS X code</p></p></span>
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