Concern:
Xcode comes with inbuilt code snippet library to provide code skeleton to implement repeatedly used code quickly.Fortunately we can update this list of snippets in library and add our own custom snippet which we need most of the time in our life!
Outcome:
Our own custom snippet to save implementation time.Description:
we have been using many of the snippet in Xcode which makes it easier to implement code like for, while, switch and most used init code snippet.Let's add our own custom code snippet, by taking example NSString method -
+ (instancetype)stringWithFormat:(NSString *)format;
for example, with out code snippet we have to type all the syntax every time we want to implement this method, [NSString stringWithFormat:@"%@",];
Now lets add snippet for this,
- Type statement which you want to come up automatically on calling snippet.
- Select and Add this code to snippet library.
Select code and drag it to snippet library, which can be activated by View > Utilizes > Show Code Snippet Library or press control + option + cmd + 2 in Xcode.
On success, we will find New snippet added in library named "My Code Snippet".
- Edit snippet
Click newly added snippet to activate snippet summery pop up.
Click on edit, which will allow to modify snippet more specifically.
- Title, is a snippet name by which that snippet will be called
- Summery, which shows brief function of snippet
- Platform, allows to select platform OS X or iOS or Both
- Language, allows to specify language snippet belongs to
- Completion Shortcut, allows to set shortcut for snippet
- Completion Scope, allows to set scope, where snippet will be available
- For our example,
Title : strWithFormate
Summary : snippet for stringWithFormat
Platform : All
Language : Objective C
Completion Shortcut : strWithFormate
Completion Scope : All
Language : Objective C
Completion Shortcut : strWithFormate
Completion Scope : All
- Add bubble to accept values
edit snippet code by,
[NSString stringWithFormat:@"<#type#>", <#expression#>];
which adds bubble to accept values for data type and expression we are going to log.
which adds bubble to accept values for data type and expression we are going to log.
- Done!
Now if we type strWithFormate and hit enter, we will get
[NSString stringWithFormat:@"<#type#>", <#expression#>];
and by pressing TAB, we will able to assign values to pre populated bubbles.
Conclusion:
Before:
After:
Comments / Suggestions are welcomed!
No comments :
Post a Comment