If you are a developer in the development process will find some code, whether in the same project or in different projects, the usage will be very high, experienced people will be directly encapsulated in a class, or write a macro definition or collect the code, the next time directly to use, or put into the code library of Xcode, Direct use, thus improving the development efficiency;1. Encapsulate common code snippets into a class
When a piece of code is often present in one or more projects, it is encapsulated in a class, which can be used to implement the function directly when it is used, or to put the class directly into another project.
Examples of using Uialertview
Create a Xf_uikit class for declaring files and implementing files as
xf_uikit.m// demo//// Created by DolBy on 13-6-17.// Copyright (c) 2013 fresh air wave. All rights reserved.//#import "Xf_uikit.h" @implementation xf_uikit+ (void) Showalert: (NSString *) title Withmessage: ( NSString *) message witchcancelbuttontitle: (NSString *) cancelbuttontitle otherbuttontitles: (NSString *) Otherbuttontitles, ... ns_requires_nil_termination{ Uialertview *alert = [[Uialertview alloc] Initwithtitle:title message:message Delegate:self Cancelbuttontitle:cancelbuttontitle otherbuttontitles:otherbuttontitles, nil]; [Alert show]; [Alert release];} @end
direct call to class method when using a header file
-(void) viewdidload{ [Super Viewdidload]; [Xf_uikit showalert:@ "Warning" withmessage:@ "wind wave test" witchcancelbuttontitle:@ "OK" otherbuttontitles:nil]; Do any additional setup after loading the view, typically from a nib.}
Create a Xf_uikit class for declaring files and implementing files as
2. Using macros
In the iOS development of those high-efficiency commonly used macros For example, the use of macros, not mentioned here;
3. Using Xcode to bring a code snippet library
Under the Properties panel there is a column library panel selection bar, and there is a code Snippet library with iOS os X and User (custom) snippet Storage
(1) In these libraries have the system comes with code snippets, when using the direct drag to the project, fill in the parameters can be used.(2) User-defined code block, which is the focus of this article. For example:
have done the development of all know to use the expression diagram cells are more frequent, often write their delegate method frequently, if you collect them
#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 to draw a basic representation of the diagram unit must implement the Protocol method, the full selection of the code to the Snippet library, such as
Then will pop up an edit box, and then make a simple edit, title represents you this code block title, Summary to this piece of code, a simple introduction, completion shortcut express a shortcut key, in our project as long as the input shortcut key can display the whole piece of code;
corresponding settings
Put the argument between the two # numbers, like #参数 #
Finished editing select done, you can see the Tbaleview code block under User
There are two ways to use it.
① the code block directly onto the project;
② in the project to enter their own set of express construction code, such as the Xftableview just set, the whole code block appears;
In the Repository (libary)/developer/xcode/userdata/codesnippets is your custom code snippet, if you re-install Xcode remember to backup Oh!
iOS Development Tips--reusing code snippets