Xcode:添加自定义代码片段
2017-01-18 本文已影响146人
萌小奇
在Xcode的库面板——代码片段库 中,可以添加自定义的使用率比较高代码块
添加方法:
-
通过快捷键(control+option+command+2)打开code snippet library, 在Xcode屏幕的右下角(Xcode改版后,版面发生了改变)
更改后的打开位置:
打开代码块.png
打开代码块.png
- 选中你的代码片段拖到code sniper library中,并设置title,快捷键,作用域
- 就是直接使用了,直接敲设置的快捷键就会弹出提示,直接回车就出出现你的代码片段
自己使用的如下:
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;