在Xcode的庫面板——代碼片段庫 中激率,可以添加自定義的使用率比較高代碼塊
添加方法:
-
通過快捷鍵(control+option+command+2)打開code snippet library, 在Xcode屏幕的右下角(Xcode改版后,版面發(fā)生了改變)
更改后的打開位置:
或者直接點(diǎn)解右上角{}
- 創(chuàng)建你需要常用代碼塊
在 Xcode 內(nèi)鞋囊,先寫上這樣一段占位符代碼苟穆,寫在.h或是.m中威酒。
@property(nonatomic, strong) <#type#> * <#name#>;
- 選中你的代碼片段拖到code sniper library中,并設(shè)置title,快捷鍵,作用域史侣。
或者選中所寫代碼塊,右鍵單擊出現(xiàn)菜單魏身,選擇Create Code Snippet
惊橱,然后設(shè)置title,快捷鍵箭昵,作用域税朴。
如圖:
Title:代碼片段的名稱 (在代碼片段庫的列表中,以及敲擊快捷方式時家制,會出現(xiàn) Title 內(nèi)所填寫內(nèi)容)正林。
Summary:對代碼片段的功能的簡要描述 (可寫可不寫,僅僅會在代碼片段庫列表中顯示)颤殴。
Platform:將代碼片段的可見性(即觅廓,可用快捷方式調(diào)用的)限制為指定的平臺,可以選擇 All涵但,也可以選擇別的杈绸,根據(jù)需要而定。
Language:將代碼片段的可見性(即矮瘟,可用快捷方式調(diào)用的)限制為指定的語言瞳脓。 根據(jù)你自己寫的而定。最常見的是 C澈侠、 Objective-C劫侧、 C++ 或 Objective-C++。
Completion Shortcut:使用代碼片段時的快捷方式哨啃。 舉個例子烧栋,比如這里填寫的是 @strong,在程序中打出 @strong棘催,就會顯示代碼塊的內(nèi)容劲弦。對于常用的片段,快捷方式應(yīng)該相對較短醇坝。Xcode 不會警告存在沖突 / 重疊的快捷方式邑跪,所以確保你的快捷方式不會與現(xiàn)有的快捷方式重疊次坡。
Completion Scopes:將代碼片段的可見性(即,可用快捷方式調(diào)用的)限制為指定的范圍画畅。 舉個例子砸琅,if / else 語句只能在方法或函數(shù)實(shí)現(xiàn)中自動完成。
- 就是直接使用了轴踱,直接敲設(shè)置的快捷鍵就會彈出提示症脂,直接回車就出出現(xiàn)你的代碼片段
自己使用的如下:
Title | Completion Shortcut | Code |
---|---|---|
@property (strong) | propertystrong | @property (nonatomic, strong) <#type#> *<#name#>; |
@property (weak) | propertyweak | @property (nonatomic, weak) <#type#> *<#name#>; |
@property(assign) | propertyassign | @property (nonatomic, assign) <#Type#> <#type#> |
@property(block) | propertyblock | @property (nonatomic, copy) void(^<#Type#>)(<#type#>); |
@property(copy) | propertycopy | @property (nonatomic, copy) <#Type#> *<#type#> |
/** **/ | ss | /< <#注釋#> >/ |
#pragma mark | pm | #pragma mark - <#delegate#> |
////////
ff 分隔行
///////////////////////////////////////////////////////////////////////////////
/** */
dd 多行注釋
dd
/**
* <##>
*/
創(chuàng)建單例
shareInstance
+ (instancetype)shareInstance {
static dispatch_once_t onceToken;
static id _singleton_;
dispatch_once(&onceToken, ^{
_singleton_ = [[self alloc] init];
});
return _singleton_;
}
block
typefblock
void (^<#Title#>)(<#Type#> <#type#>) = ^(<#Type#> <#type#>){
};
CollectionDataSource
CollectionDataSource
CollectionSource
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return <#NSInteger#>;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cid forIndexPath:indexPath];
return cell;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return <#NSInteger#>;
}
CollectionDelegate
collectijonviewdelegate
#pragma mark - collectionViewDelegate
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(0, 0);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"%ld",indexPath.row);
}
Notification Add
na
[[NSNotificationCenter defaultCenter] addObserver:<#Observer#> selector:<#Selector#> name:<#Name#> object:<#Object#>];
Notification Remove
nr
[[NSNotificationCenter defaultCenter] removeObserver:<#Observer#> name:<#Name#> object:<#Object#>];
NSLog
log
NSLog(@"<#Log#>");
Private Interface
interface
@interface <#Title#> ()
@end
TableView
createTableView
tablesource
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return <#NSInteger#>;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return <#NSInteger#>;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
return <#UITableViewCell#>;
}
Tableview分割線
tableviewseparate
//view布局完子控件的時候調(diào)用
- (void)viewDidLayoutSubviews
{
//iOS7只需要設(shè)置SeparatorInset(iOS7開始有的)就可以了,但是iOS8的話單單只設(shè)置這個是不行的淫僻,還需要設(shè)置LayoutMargins(iOS8開始有的)
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
}
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[self.tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
}
}
//cell即將展示的時候調(diào)用
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
weakSelf
weak&&strong
weak__
#define SHWeakSelf(type) __weak typeof(type) weak##type = type;
#define SHStrongSelf(type) __strong typeof(type) type = weak##type;
- 代碼片段備份:
Xcode 中的代碼片段默認(rèn)放在下面的目錄中:
~/Library/Developer/Xcode/UserData/CodeSnippets
可以將目錄中的代碼片段備份诱篷,也可以將其直接拷出來放在不同的電腦上使用。