在Xcode的庫面板——代碼片段庫 中,可以添加自定義的使用率比較高代碼塊
添加方法:
-
通過快捷鍵(control+option+command+2)打開code snippet library, 在Xcode屏幕的右下角(Xcode改版后,版面發生了改變)
更改后的打開位置:
或者直接點解右上角{}
- 創建你需要常用代碼塊
在 Xcode 內,先寫上這樣一段占位符代碼,寫在.h或是.m中。
@property(nonatomic, strong) <#type#> * <#name#>;
- 選中你的代碼片段拖到code sniper library中,并設置title,快捷鍵,作用域。
或者選中所寫代碼塊,右鍵單擊出現菜單,選擇Create Code Snippet
,然后設置title,快捷鍵,作用域。
如圖:
Title:代碼片段的名稱 (在代碼片段庫的列表中,以及敲擊快捷方式時,會出現 Title 內所填寫內容)。
Summary:對代碼片段的功能的簡要描述 (可寫可不寫,僅僅會在代碼片段庫列表中顯示)。
Platform:將代碼片段的可見性(即,可用快捷方式調用的)限制為指定的平臺,可以選擇 All,也可以選擇別的,根據需要而定。
Language:將代碼片段的可見性(即,可用快捷方式調用的)限制為指定的語言。 根據你自己寫的而定。最常見的是 C、 Objective-C、 C++ 或 Objective-C++。
Completion Shortcut:使用代碼片段時的快捷方式。 舉個例子,比如這里填寫的是 @strong,在程序中打出 @strong,就會顯示代碼塊的內容。對于常用的片段,快捷方式應該相對較短。Xcode 不會警告存在沖突 / 重疊的快捷方式,所以確保你的快捷方式不會與現有的快捷方式重疊。
Completion Scopes:將代碼片段的可見性(即,可用快捷方式調用的)限制為指定的范圍。 舉個例子,if / else 語句只能在方法或函數實現中自動完成。
- 就是直接使用了,直接敲設置的快捷鍵就會彈出提示,直接回車就出出現你的代碼片段
自己使用的如下:
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
/**
* <##>
*/
創建單例
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布局完子控件的時候調用
- (void)viewDidLayoutSubviews
{
//iOS7只需要設置SeparatorInset(iOS7開始有的)就可以了,但是iOS8的話單單只設置這個是不行的,還需要設置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即將展示的時候調用
- (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 中的代碼片段默認放在下面的目錄中:
~/Library/Developer/Xcode/UserData/CodeSnippets
可以將目錄中的代碼片段備份,也可以將其直接拷出來放在不同的電腦上使用。