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 .}
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.
(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
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;
Settings
Place the parameter between two # numbers, for example, # parameter #
After editing, select done. The tbaleview code block is displayed under the user.
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;
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!
Original blog welcome to repost share, please indicate the source of http://blog.csdn.net/duxinfeng2010