If you are a developer, during the development process, you will find that some code is frequently used in the same project or in different projects, experienced people will directly encapsulate the code in a class, or write a macro definition or collect the code. They will directly use it next time, or put it in the xcode code library for direct use, to improve development efficiency;
1. encapsulate common code snippets into a class
When a piece of code appears frequently in one or more projects, it is encapsulated in a class. When used, you can directly PASS Parameters to implement functions, or directly put this type in another project for the same use;
Example of using UIAlertView
Create an XF_UIKit class.
//// XF_UIKit.h // Demo /// Created by DolBy on 13-6-17. // Copyright (c. all rights reserved. // # import <Foundation/Foundation. h> @ interface XF_UIKit: NSObject + (void) showAlert :( NSString *) title withMessage :( NSString *) message witchCancelButtonTitle :( NSString *) cancelButtonTitle encrypted :( NSString *) otherButtonTitles ,... NS_REQUIRES_NIL_TERMINATION; @ end
//// XF_UIKit.m // Demo /// Created by DolBy on 13-6-17. // Copyright (c. all rights reserved. // # import "events" @ implementation XF_UIKit + (void) showAlert :( NSString *) title withMessage :( NSString *) message witchCancelButtonTitle :( NSString *) cancelButtonTitle otherButtonTitles :( NSString *) otherButtonTitles ,... detail {UIAlertView * alert = [[UIAlertView alloc] initWithTitle: title message: message delegate: self cancelButtonTitle: cancelButtonTitle detail: otherButtonTitles, nil]; [alert show]; [alert release];} @ end
Introduce the header file to call the class method directly.
-(Void) viewDidLoad {[super viewDidLoad]; [XF_UIKit showAlert: @ "warning" withMessage: @ "Test of wind and waves" witchCancelButtonTitle: @ "OK" otherButtonTitles: nil]; // Do any additional setup after loading the view, typically from a nib .}
650) this. width = 650; "src =" http://img.blog.csdn.net/20130617153744203 "_ xhe_src =" http://img.blog.csdn.net/20130617153744203 "style =" max-width: 98%; font-family: arial, helvetica, sans-serif; font-size: 12px; background-color: # ffffff; "/>
2. Use macros
In iOS development, the frequently-used macros are used as examples;
3. Use Xcode's own code snippet Library
Under the attribute panel, there is a Library panel selection bar. There is a Code Snippet Library that stores OS X and User (custom) Code snippets in iOS.
650) this. width = 650; "src =" http://img.blog.csdn.net/20130617154934796 "_ xhe_src =" http://img.blog.csdn.net/20130617154934796 "style =" max-width: 98%; "/>
(1) There are built-in code snippets in these libraries. When using them, drag them directly to the project and fill in the parameters to use them. (2) User-Defined code blocks, which are also the focus of this article. For example:
All the developers know that they use the delegate method to indicate that graph cells are frequently used. If they are collected
#pragma mark -#pragma mark UITableViewDataSource and UITableViewDelegate Methods-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1;}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 10;}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } return cell;}
This is a protocol method that is required to draw a basic graph unit. Select all the Code and drag it to the Code Snippet Library, as shown in figure
650) this. width = 650; "src =" http://img.blog.csdn.net/20130620142958593 "_ xhe_src =" http://img.blog.csdn.net/20130620142958593 "style =" max-width: 98%; "/>
Then, an edit box will pop up, and a simple edit will be made. title indicates the title of your code block. Summary briefly introduces this code piece, and Completion Shortcut indicates a Shortcut key, in our project, you only need to enter the shortcut key to display the entire code segment;
650) this. width = 650; "src =" http://img.blog.csdn.net/20130620143003859 "_ xhe_src =" http://img.blog.csdn.net/20130620143003859 "style =" max-width: 98%; "/>
Settings
650) this. width = 650; "src =" http://img.blog.csdn.net/20130620143822421 "_ xhe_src =" http://img.blog.csdn.net/20130620143822421 "style =" max-width: 98%; "/>
Place the parameter between two # numbers, for example, # parameter #
650) this. width = 650; "src =" http://img.blog.csdn.net/20130620143827312 "_ xhe_src =" http://img.blog.csdn.net/20130620143827312 "style =" max-width: 98%; "/>
After editing, select Done. The TbaleView code block is displayed under the User.
650) this. width = 650; "src =" http://img.blog.csdn.net/20130620144227578 "_ xhe_src =" http://img.blog.csdn.net/20130620144227578 "style =" max-width: 98%; "/>
Two methods are available.
① Drag the code block directly to the project;
② Enter the self-configured parcel creation code in the project. For example, if the XFTableView is just set, all the real code blocks will appear;
650) this. width = 650; "src =" http://img.blog.csdn.net/20130620144538000 "_ xhe_src =" http://img.blog.csdn.net/20130620144538000 "style =" max-width: 98%; "/>
Your custom code snippets are stored in the resource library (Libary), Developer/Xcode/UserData/CodeSnippets. If you reinstall Xcode, remember to back up your code!
650) this. width = 650; "src =" http://img.blog.csdn.net/20130620145808953 "_ xhe_src =" http://img.blog.csdn.net/20130620145808953 "style =" max-width: 98%; "/>
This article from the "fresh wind wave" blog, please be sure to keep this source http://duxinfeng.blog.51cto.com/3911492/1225867