iOS效率_代码块

代码块.png

Title:表示代码块的标题;
Summary:表示代码块的描述信息;
Platform:表示代码块可以使用的平台,默认即可;
Language:表示代码块可以使用的语言平台;
Completion Shortcut:表示代码块的快捷方式;
Completion Scopes:表示代码块的使用范围;

创建方法Xcode11,鼠标右键选择Create Code Snippet,添加以下代码块常用方法。

代码块常用方法

注释

方法集注释
有线注释
#pragma mark - <#注释#>
无线注释
#pragma mark <#mark#>

普通注释1
/** <#注释#> */

///< <#注释#>

普通注释2
/** <#注释#>
 *  <#注释#>
 */

方法注释
/**
 <#注释#>

 @param <#注释#>        <#注释#>
 @param <#注释#>        <#注释#>
 @return <#注释#>       <#注释#>
 */

未写完代码
//TODO:<#info#>

警告
#warning <#message#>

需要修改的地方
// FIXME: <#Fix详情#>

@property属性声明

@property (nonatomic, <#copy#>) <#NSString#> *<#name#>; /** <#注释#> */
@property(nonatomic,strong) <#Class#> *<#object#>;
@property(nonatomic,assign) <#Class#> <#property#>;
@property(nonatomic,copy) NSString *<#name#>;
@property(nonatomic,weak)id<<#protocol#>> <#delegate#>;
@property(nonatomic,copy) <#Block#> <#block#>;
@property(nonatomic,copy) <#class#> (^<#block#>)(id<#object1#>,...);

枚举

typedef NS_ENUM(NSInteger, <#TypeName#>) {
    
};

typedef NS_OPTIONS(NSUInteger, <#TypeName#>) {
                
};

if DeBug

#if DEBUG
<#TODO#>
#else
<#TODO#>
#endif

单例

static <#SingleObject#> *_singleInstance = nil;
+(instancetype)sharedInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (_singleInstance == nil) {
            _singleInstance = [[self alloc]init];
        }
    });
    return _singleInstance;
}

+(instancetype)allocWithZone:(struct _NSZone *)zone{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _singleInstance = [super allocWithZone:zone];
    });
    return _singleInstance;
}

-(id)copyWithZone:(NSZone *)zone{
    return _singleInstance;
}

-(id)mutableCopyWithZone:(NSZone *)zone {
    return _singleInstance;
}

懒加载

- (<#NSMutableArray *#>)<#name#> {
    if (!<#name#>) {
        <#name#> = <#[NSMutableArray array]#>;
    }
    return <#name#>;
}

圆角边框

<#view#>.layer.borderWidth = 1.0;
<#view#>.layer.borderColor = [UIColor grayColor].CGColor;
<#view#>.layer.cornerRadius = 10;
<#view#>.layer.masksToBounds = YES;

UIButton创建

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 44, 44);
[button setTitle:@"<#标题#>" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.backgroundColor = <#[UIColor blueColor]#>;
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[<#self.view#> addSubview:button];

UITableView创建

#pragma mark —————<#UITableView#>—————
- (void)initWithTableView{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = <#50#>;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"<#nibName#>" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"<#cellID#>"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1 ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    <#UITableViewCell#> *cell = [tableView dequeueReusableCellWithIdentifier:@"<#cellID#>" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

TableViewCell

staticNSString *reuseID = <#property#>;

<#class#> *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];

if(!cell) {

cell = [[<#class#> alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseID];

}

UICollectionView创建

#pragma mark —————<#UICollectionView>#—————
- (void)initWithCollectionView{
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(10, 10);
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    flowLayout.minimumLineSpacing = 5;
    flowLayout.minimumInteritemSpacing = 5;
    self.collectionView.collectionViewLayout = flowLayout;
    [self.collectionView registerNib:[UINib nibWithNibName:@"<#nibName#>" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"<#cellId#>"];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return 10;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    <#UICollectionViewCell#> * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"<#cellId#>" forIndexPath:indexPath];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
}

代理

@protocol <#ViewControllerDelegate#> 

@required // 必须实现的方法 默认

@optional // 可选实现的方法
- (void)<#ViewControllerDidSuccess#>:(<#Class#> *)<#obj#>;

@end

@property(nonatomic, weak) id<<#ViewControllerDelegate#>> <#代理属性#>;

//成功后的回调
    if ([self.<#代理属性#> respondsToSelector:@selector(<#代理方法名ViewControllerDidSuccess#>:)]) {
        [self.<#代理属性#> <#代理方法名ViewControllerDidSuccess#>:<#obj#>];
    }

属性Block

// <#属性block#>
@property (nonatomic, copy) void(^<#nameBlock#>)(<#Class#> *<#obj#>);

if (self.<#nameBlock#>) {
    self.<#nameBlock#>(<#obj#>);
}

方法Block

typedef void(^<#AYHandle#>)(<#Class#> *<#obj#>);

- (void)<#block方法名#>:(<#AYHandle#>)<#xxHandle#>;

/** 方法block回调 */
@property (nonatomic, copy) <#AYHandle#> <#xxHandle#>;

if (self.<#xxHandle#>) {
    self.<#xxHandle#>(<#obj#>);
}

- (void)<#block方法名#>:(<#AYHandle#>)<#xxHandle#> {
    self.<#xxHandle#> = <#xxHandle#>;
}

语法糖(单独有一章节介绍语法糖)

创建view

({

UIView*view = [[UIView alloc]init];

[<#code#> addSubview:view];

view.backgroundColor = [UIColor <#code#>];

[view mas_makeConstraints:^(MASConstraintMaker *make) {

<#code#>

}];

view;

});


带点击事件的View
({

UIView*view = [[UIView alloc]init];

[<#code#> addSubview:view];

view.backgroundColor = [UIColor <#code#>];

[view mas_makeConstraints:^(MASConstraintMaker *make) {

<#code#>

}];

[view whenTapped:<#^(void)block#>];

view;

});

带点击事件的Label
({

UILabel*label = [[UILabel alloc]init];

label.text = <#code#>;

label.textColor = [UIColor <#code#>];

label.font = [UIFont systemFontOfSize:<#code#>];

[<#code#> addSubview:label];

[label mas_makeConstraints:^(MASConstraintMaker *make) {

<#code#>

}];

[label whenTapped:<#^(void)block#>];

label;

});

手势TapGestureRecognizer

UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(<#NSObject#>)];
[<#NSObject#> addGestureRecognizer:tap];

Xcode迁移代码块

Xcode中的代码块默认路径是:
~/Library/Developer/Xcode/UserData/CodeSnippets
可以将路径中的代码块,迁移到不同的电脑上使用,需重新启动Xcode ;

代码块作用域

代码块作用域.jpg

你可能感兴趣的:(iOS效率_代码块)