常用的Code Snippets

Code Snippets

通過code snippets,我們可以創(chuàng)建一些可重用的代碼塊,并且在任何需要的地方很容易的就可以使用這些代碼塊。

NSHipster - code snippets中有不少code sinppets鬼譬。

另外引有,我們可能常常需要轉(zhuǎn)移或者同步到別的電腦上,那么我們就需要知道這些Code Snippets存放的位置:

~/Library/Developer/Xcode/UserData/CodeSnippets

知道這些文件在哪,其他就好辦了蜂桶。

接下來記錄項(xiàng)目當(dāng)中常用的一些code snippets:

__strong self

  • Title:__strong self
  • Summary:Declare strong reference to weak reference
  • Completion Shorcut:_strong self
  • Completion Scopes:Function or Method
__strong __typeof(<#weakSelf#>)strongSelf = <#weakSelf#>;

__weak self

  • Title:__weak self
  • Summary:Declare weak reference to strong referencee
  • Completion Shorcut:__weak self
  • Completion Scopes:Function or Method
__weak __typeof(self) <#weakSelf#> = self;

Annotation

  • Title:Annotation Pandect
  • Summary:注釋
  • Completion Shorcut:annotation
  • Completion Scopes:All
/**
 * <#annotation#>
 */

Warning

  • Title:Warning
  • Summary:警告
  • Completion Shorcut:warn
  • Completion Scopes:All
#warning mark - <#warning#>

Assign

  • Title:Assign Property
  • Summary:declare assign property
  • Completion Shorcut:@property assign
  • Completion Scopes:Class Interface Methods
@property (nonatomic, assign) <#Object#> <#obj#>;

Readonly

  • Title:Readonly Property
  • Summary:declare readonly property
  • Completion Shorcut:@property readonly
  • Completion Scopes:Class Interface Methods
@property (nonatomic, readonly) <#Object#> *<#obj#>;

Strong

  • Title:Strong Property
  • Summary:declare strong property
  • Completion Shorcut:@property strong
  • Completion Scopes:Class Interface Methods
@property (nonatomic, strong) <#Object#> *<#obj#>;

Weak

  • Title:Weak Property
  • Summary:declare weak property
  • Completion Shorcut:@property weak
  • Completion Scopes:`Class Interface Methods
@property (nonatomic, weak) <#id#> <#obj#>;

Class Continuation

  • Title:Class Continuation
  • Summary:Anonymous category to define private methods in an implementation
  • Completion Shorcut:class continuation
  • Completion Scopes:Top Level
@interface <#Class Name#> ()

<#Continuation#>

@end

dispatch_async

  • Title:dispatch_async Pattern for Background Processing
  • Summary:Dispatch to do work in the background, and then to the main queue with the results
  • Completion Shorcut:dispatch_async
  • Completion Scopes:Function or Method
dispatch_async(dispatch_get_global_queue(<#dispatch_queue_priority_t priority#>, <#unsigned long flags#>), ^(void) {
        <#code#>
        
        dispatch_async(dispatch_get_main_queue(), ^(void) {
            <#code#>
        });
    });

Documents Directory Path

  • Title:Documents Directory Path
  • Summary:文件主路徑
  • Completion Shorcut:documents directory
  • Completion Scopes:Function or Method
NSURL *documentsDirectoryURL = [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]];

ImageView

  • Title:ImageView
  • Summary:Create & Initialize UIImageView with Named Image
  • Completion Shorcut:imageNamed
  • Completion Scopes:Code Expression
[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"<#image name#>"]]

init

  • Title:init
  • Summary:Designated incantation for your designated initializers
  • Completion Shorcut:init self
  • Completion Scopes:Function or Method
self = [super init];
    if (!self) {
        return nil;
    }
    
    <#initializations#>
    
    return self;

NSCoding Protocol Methods

  • Title:NSCoding Protocol Methods
  • Summary:Placeholders for NSCoding protocol methods
  • Completion Shorcut:nscoding protocol
  • Completion Scopes:Class Implementation
#pragma mark - NSCoding

- (instancetype)initWithCoder:(NSCoder *)decoder {
    self = [super init];
    if (!self) {
        return nil;
    }
    
    <# implementation #>
    
    return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
    <# implementation #>
}

NSLocalizedString

  • Title:NSLocalizedString
  • Summary:本地化
  • Completion Shorcut:local string
  • Completion Scopes:Code Expression
 NSLocalizedString(@"<#Message#>", <#Comment#>)

Pragma Mark

  • Title:Pragma Mark
  • Summary:標(biāo)記
  • Completion Shorcut:mark
  • Completion Scopes:Class Interface MethodsClass Implementation也切、Top Level
#pragma mark - <#mark#>

Remove Notification Observer

  • Title:Remove Notification Observer
  • Summary:移除notification的觀察者
  • Completion Shorcut:remove observer
  • Completion Scopes:Function or Method
[[NSNotificationCenter defaultCenter] removeObserver:self];

Set Frame

  • Title:Set Frame
  • Summary:Initializes a view frame inside a code block
  • Completion Shorcut:set view frame
  • Completion Scopes:Function or Method
<# view #>.frame = ({
        CGRect frame = <# view #>.frame;
        <# ... #>
        frame;
    });

Shared Singleton

  • Title:Shared Singleton
  • Summary:Class method that returns a singleton instance
  • Completion Shorcut:shared singleton
  • Completion Scopes:Class Implementation
+ (instancetype)shared<#name#> {
    static <#class#> *_shared<#name#> = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _shared<#name#> = <#initializer#>;
    });
    
    return _shared<#name#>;
}

Specta Test Case

  • Title:Specta Test Case
  • Summary:declare a speca test case
  • Completion Shorcut:test case
  • Completion Scopes:Top level
describe(<#NSString *name#>, ^{
    <#code#>
    
    context(<#NSString *name#>, ^{
        
        beforeEach(^{
            <#code#>
        });
        
        it(<#NSString *name#>, ^{
            <#code#>
        });
    });
    
});

String Format

  • Title:String Format
  • Completion Shorcut:string format
  • Completion Scopes:Function or Method
[NSString stringWithFormat:<#(nonnull NSString *), ...#>]

UIPickerViewDataSource

  • Title:UIPickerViewDataSource
  • Summary:Placeholders for required UIPickerView datasource methods
  • Completion Shorcut:picker view datasource
  • Completion Scopes:Class Implementation
#pragma mark - UIPickerDataSource

- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
    return <#number#>
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return <#number#>
}

UIPickerViewDelegate

  • Title:UIPickerViewDelegate
  • Summary:Placeholders for required UIPickerView Delegate methods
  • Completion Shorcut:picker view delegate
  • Completion Scopes:Class Implementation
#pragma mark - UIPickerViewDelegate

- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row
            forComponent:(NSInteger)component
{
    <#code#>
}

- (void)pickerView:(UIPickerView *)pickerView
      didSelectRow:(NSInteger)row
       inComponent:(NSInteger)component
{
    <#code#>
}

UITableViewDataSource

  • Title:UITableViewDataSource
  • Summary:Placeholders for required UITableViewDataSource delegate methods
  • Completion Shorcut:table view datasource
  • Completion Scopes:Class Implementation
#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return <#number#>;
}

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
    return <#number#>;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#reuseIdentifier#> forIndexPath:<#indexPath#>];
    
    [self configureCell:cell forRowAtIndexPath:indexPath];
    
    return cell;
}

- (void)configureCell:(UITableViewCell *)cell
    forRowAtIndexPath:(NSIndexPath *)indexPath
{
    <#statements#>
}

UITableViewDelegate

  • Title:UITableViewDelegate
  • Summary:Placeholders for required UITableViewDelegate protocol methods
  • Completion Shorcut:table view didselect
  • Completion Scopes:Class Implementation
#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    <#statements#>
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末扑媚,一起剝皮案震驚了整個(gè)濱河市腰湾,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌疆股,老刑警劉巖费坊,帶你破解...
    沈念sama閱讀 221,548評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異旬痹,居然都是意外死亡附井,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門两残,熙熙樓的掌柜王于貴愁眉苦臉地迎上來永毅,“玉大人,你說我怎么就攤上這事人弓≌铀溃” “怎么了?”我有些...
    開封第一講書人閱讀 167,990評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵崔赌,是天一觀的道長(zhǎng)意蛀。 經(jīng)常有香客問我,道長(zhǎng)健芭,這世上最難降的妖魔是什么县钥? 我笑而不...
    開封第一講書人閱讀 59,618評(píng)論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮吟榴,結(jié)果婚禮上魁蒜,老公的妹妹穿的比我還像新娘。我一直安慰自己吩翻,他們只是感情好兜看,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,618評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著狭瞎,像睡著了一般细移。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上熊锭,一...
    開封第一講書人閱讀 52,246評(píng)論 1 308
  • 那天弧轧,我揣著相機(jī)與錄音,去河邊找鬼碗殷。 笑死精绎,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的锌妻。 我是一名探鬼主播代乃,決...
    沈念sama閱讀 40,819評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了搁吓?” 一聲冷哼從身側(cè)響起原茅,我...
    開封第一講書人閱讀 39,725評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎堕仔,沒想到半個(gè)月后擂橘,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,268評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡摩骨,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,356評(píng)論 3 340
  • 正文 我和宋清朗相戀三年通贞,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片恼五。...
    茶點(diǎn)故事閱讀 40,488評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡滑频,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出唤冈,到底是詐尸還是另有隱情峡迷,我是刑警寧澤,帶...
    沈念sama閱讀 36,181評(píng)論 5 350
  • 正文 年R本政府宣布你虹,位于F島的核電站绘搞,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏傅物。R本人自食惡果不足惜夯辖,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,862評(píng)論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望董饰。 院中可真熱鬧蒿褂,春花似錦、人聲如沸卒暂。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽也祠。三九已至昙楚,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間诈嘿,已是汗流浹背堪旧。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評(píng)論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留奖亚,地道東北人淳梦。 一個(gè)月前我還...
    沈念sama閱讀 48,897評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像昔字,于是被迫代替她去往敵國和親爆袍。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,500評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容

  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa閱讀 8,878評(píng)論 0 6
  • 不一樣的角度看夜景 不一樣的角度看倒影 不一樣的角度看街景 同樣的事物從不同角度看就會(huì)有不一樣的感官體驗(yàn)。 不妨試試螃宙。
    楚木cm閱讀 178評(píng)論 0 1
  • 夏天網(wǎng)絡(luò)傳媒招收女主播.底薪3000一天6小時(shí)客叉,底薪2000一天4小時(shí)灶体,收入+名氣 輕松+愉快
    MC夏天閱讀 207評(píng)論 0 0
  • box-sizing屬性值有2個(gè),border-box 和 content-box浙于,實(shí)例: 此時(shí)的效果 看盒模型 ...
    RelaxedAndHappy閱讀 356評(píng)論 0 0
  • 第6章 真實(shí)的表達(dá)自己的想法與情緒 職場(chǎng)如戰(zhàn)場(chǎng)芹助,當(dāng)今社會(huì)壓力大堂湖,競(jìng)爭(zhēng)強(qiáng)烈,幾乎大部分的職場(chǎng)人都戰(zhàn)戰(zhàn)兢兢...
    sunshinewang閱讀 750評(píng)論 0 0