目录
一、常用代码片段1、assign - @assign2、copy - @copy3、strong - @strong4、intererface - @interface5、lazyLoad - @lazyLoad6、initView - @initView7、initTVCell - @initTVCell8、initTableView - @initTableView9、initCollectionView - @initCollectionView
二、代码片段迁移1、直接迁移文件夹2、远程仓库管理
以下是个人长期习惯使用的代码片段,是承受了个人的代码习惯的多次演变的而总结出来的。
正所谓飘飘树叶千千万,也找不出两片是一样的,但是可以找到我们认为最美的那片叶子。同样,每个人的编码风格都会有所不懂,都希望找出自己所认为的简单明了的最方便的编码风格。
没有长期固定的样式,只有在不断的优化中寻找真我。
下面就是代码片段的截图样式,标题的前后两个分别标注:title(标题)、Completion Shortcut(快捷方式)
一、常用代码片段
1、assign - @assign
@property (nonatomic
, assign
) <#class#
> <#name#
>;
2、copy - @copy
@property (nonatomic
, copy
) <#class#
> *<#name#
>;
3、strong - @strong
@property (nonatomic
, strong
) <#class#
> *<#name#
>;
4、intererface - @interface
@interface <#class#
> ()
@end
5、lazyLoad - @lazyLoad
-(<#class#
> *) <#name#
> {
if (!_
<#name#
>) {
_
<#name#
> = [<#class#
> new
];
}
return _
<#name#
>;
}
6、initView - @initView
#pragma mark - 赋值
#pragma mark - Methods
#pragma mark - Intial
- (instancetype
)initWithCoder
:(NSCoder
*)aDecoder
{
if (self = [super initWithCoder
: aDecoder
]) {
[self setUpBaseData
];
[self setUpUI
];
}
return self;
}
- (instancetype
)initWithFrame
:(CGRect
)frame
{
if (self = [super initWithFrame
:frame
]) {
[self setUpBaseData
];
[self setUpUI
];
}
return self;
}
- (void)setUpBaseData
{
}
- (void)setUpUI
{
}
#pragma mark - lazyLoad
- (void)dealloc
{
}
7、initTVCell - @initTVCell
@interface <#class#
> ()
@end
@implementation <#class#
>
#pragma mark - 赋值
#pragma mark - Methods
#pragma mark - Intial
- (instancetype
)initWithStyle
:(UITableViewCellStyle
)style reuseIdentifier
:(NSString
*)reuseIdentifier
{
if (self = [super initWithStyle
:style reuseIdentifier
:reuseIdentifier
]) {
[self setUpBaseData
];
[self setUpUI
];
}
return self;
}
- (void)setUpBaseData
{
}
- (void)setUpUI
{
}
#pragma mark - 布局
- (void)layoutSubviews
{
[super layoutSubviews
];
}
#pragma mark - lazyLoad
@end
8、initTableView - @initTableView
#import "ZM_YYLabel_TVCell.h"
#import "<#modelClass#>.h"
@interface <#class#
> ()<UITableViewDelegate
,UITableViewDataSource
>
@property(nonatomic
,strong
)UITableView
*tableView
;
@property (nonatomic
,strong
) <#ClassName#
> *model
;
@end
@implementation <#class#
>
#pragma mark -
#pragma mark - Initial
-(void)viewWillAppear
:(BOOL
)animated
{
[super viewWillAppear
:animated
];
self.title
= @"<#titleName#>";
}
- (void)viewDidLoad
{
[super viewDidLoad
];
}
-(void)firstUpdateView
{
self.view
.backgroundColor
= BACKCOLOR
;
[self tableView
];
[self registTVCell
];
}
-(void)registTVCell
{
[_tableView registerClass
:[UITableViewCell class
] forCellReuseIdentifier
:NSStringFromClass([UITableViewCell class
])];
[_tableView registerNib
:[UINib nibWithNibName
:NSStringFromClass([<#class#
> class
]) bundle
:nil
]
forCellReuseIdentifier
:NSStringFromClass([<#class#
> class
])];
}
-(void)firstLoadData
{
}
#pragma mark -
#pragma mark - Request
-(void)sendAfNetwork
{
self.model
= [<#ModelClass#
> shareDicModel
:@{}];
self.dataMarr
= [self.model
<#method#
>];
[self.tableView reloadData
];
}
#pragma mark -
#pragma mark - Methods
-(void)tableViewCellClickAction
:(NSIndexPath
*)indexPath
{
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning
];
}
#pragma mark -
#pragma mark - tabViewDelegate
-(UITableViewCell
*)tableView
:(UITableView
*)tableView cellForRowAtIndexPath
:(NSIndexPath
*)indexPath
{
UITableViewCell
*pubCell
= [UITableViewCell new
];
<#ModelClass#
> *model
= <#Array#
>[indexPath
.row
];
if ([model
.bsReuseId isEqualToString
:NSStringFromClass([<#class#
> class
])]) {
<#class#
> *cell
=[tableView dequeueReusableCellWithIdentifier
:NSStringFromClass([<#class#
> class
]) forIndexPath
:indexPath
];
pubCell
= cell
;
}
if (pubCell
) {
pubCell
.selectionStyle
= UITableViewCellSelectionStyleNone
;
}
return pubCell
;
}
-(NSInteger
)tableView
:(UITableView
*)tableView numberOfRowsInSection
:(NSInteger
)section
{
return <#section#
>;
}
-(NSInteger
)numberOfSectionsInTableView
:(UITableView
*)tableView
{
return <#row#
>;
}
-(CGFloat
)tableView
:(UITableView
*)tableView heightForRowAtIndexPath
:(NSIndexPath
*)indexPath
{
<#ModelClass#
> *model
= <#Array#
>[indexPath
.row
];
return model
.bsHeight
;
}
-(CGFloat
)tableView
:(UITableView
*)tableView heightForHeaderInSection
:(NSInteger
)section
{
return 0.0001;
}
-(CGFloat
)tableView
:(UITableView
*)tableView heightForFooterInSection
:(NSInteger
)section
{
return 0.0001;
}
-(UIView
*)tableView
:(UITableView
*)tableView viewForHeaderInSection
:(NSInteger
)section
{
@autoreleasepool
{
UIView
*vv
=[[UIView alloc
]init
];
vv
.backgroundColor
=[UIColor clearColor
];
return vv
;
}
}
-(UIView
*)tableView
:(UITableView
*)tableView viewForFooterInSection
:(NSInteger
)section
{
@autoreleasepool
{
UIView
*vv
=[[UIView alloc
]init
];
vv
.backgroundColor
=[UIColor clearColor
];
return vv
;
}
}
-(void)tableView
:(UITableView
*)tableView didSelectRowAtIndexPath
:(NSIndexPath
*)indexPath
{
[tableView deselectRowAtIndexPath
:indexPath animated
:YES
];
[self tableViewCellClickAction
:indexPath
];
}
#pragma mark - lazyload
- (UITableView
*)tableView
{
if (!_tableView
){
_tableView
= [[UITableView alloc
] initWithFrame
:kFrame(0,0,kIphone_W
,kIphone_H
- KNaviBarH
) style
:UITableViewStylePlain
];
_tableView
.backgroundColor
= [UIColor clearColor
];
_tableView
.separatorStyle
= UITableViewCellSeparatorStyleNone
;
_tableView
.scrollEnabled
= YES
;
_tableView
.estimatedRowHeight
= 0;
_tableView
.estimatedSectionHeaderHeight
= 0;
_tableView
.estimatedSectionFooterHeight
= 0;
if (@available(iOS
11.0, *)) {
_tableView
.contentInsetAdjustmentBehavior
= UIScrollViewContentInsetAdjustmentNever
;
} else {
self.automaticallyAdjustsScrollViewInsets
= NO
;
}
_tableView
.delegate
= self;
_tableView
.dataSource
= self;
[self.view addSubview
:_tableView
];
}
return _tableView
;
}
-(<#class#
> *)model
{
if (!_model
) {
_model
= [<#class#
> new
];
}
return _model
;
}
@end
9、initCollectionView - @initCollectionView
#import "<#CVCellClass#>.h"
#import "<#ModelClass#>.h"
@interface <#Class#
> ()<UICollectionViewDelegate
, UICollectionViewDataSource
, UICollectionViewDelegateFlowLayout
>
@property (strong
, nonatomic
)UICollectionView
*collectionView
;
@property (nonatomic
,strong
) <#class#
> *model
;
@end
@implementation <#Class#
>
-(void)viewWillAppear
:(BOOL
)animated
{
[super viewWillAppear
:animated
];
}
-(void)reloadNavBarItem
{
}
- (void)viewDidLoad
{
[super viewDidLoad
];
}
#pragma mark - initial
-(void)firstLoadData
{
}
-(void)firstUpdateView
{
[self collectionView
];
[self registCollectionCell
];
}
-(void)registCollectionCell
{
[self.collectionView registerClass
:[<#cellClass#
> class
] forCellWithReuseIdentifier
:NSStringFromClass([<#cellClass#
> class
])];
}
-(void)sendAfNetwork
{
}
-(void)dealloc
{
}
#pragma mark - Requests
#pragma mark - methods
-(void)collectionViewItemClickAction
:(NSIndexPath
*)indexPath
{
}
#pragma mark - <UICollectionViewDelegate>
- (UICollectionViewCell
*)collectionView
:(UICollectionView
*)collectionView cellForItemAtIndexPath
:(NSIndexPath
*)indexPath
{
UICollectionViewCell
*gridcell
= [UICollectionViewCell new
];
<#class#
> *model
= self.dataMarr
[indexPath
.item
];
if ([model
.bsReuseId isEqualToString
:NSStringFromClass([<#class#
> class
])]) {
<#class#
> *cell
= [collectionView dequeueReusableCellWithReuseIdentifier
:NSStringFromClass([<#class#
> class
]) forIndexPath
:indexPath
];
gridcell
= cell
;
}
return gridcell
;
}
- (UICollectionReusableView
*)collectionView
:(UICollectionView
*)collectionView viewForSupplementaryElementOfKind
:(NSString
*)kind atIndexPath
:(NSIndexPath
*)indexPath
{
UICollectionReusableView
*reusableView
= nil
;
if (kind
== UICollectionElementKindSectionHeader
) {
UICollectionReusableView
*footview
= [collectionView dequeueReusableSupplementaryViewOfKind
:UICollectionElementKindSectionHeader withReuseIdentifier
:NSStringFromClass([UICollectionReusableView class
]) forIndexPath
:indexPath
];
reusableView
= footview
;
}
if (kind
== UICollectionElementKindSectionFooter
) {
UICollectionReusableView
*footview
= [collectionView dequeueReusableSupplementaryViewOfKind
:UICollectionElementKindSectionFooter withReuseIdentifier
:NSStringFromClass([UICollectionReusableView class
]) forIndexPath
:indexPath
];
reusableView
= footview
;
}
return reusableView
;
}
#pragma mark - <UICollectionViewDataSource>
- (NSInteger
) numberOfSectionsInCollectionView
:(UICollectionView
*)collectionView
{
return <#num#
>;
}
- (NSInteger
)collectionView
:(UICollectionView
*)collectionView numberOfItemsInSection
:(NSInteger
)section
{
return <#num#
>;
}
- (CGSize
)collectionView
:(UICollectionView
*)collectionView layout
:(UICollectionViewLayout
*)collectionViewLayout referenceSizeForHeaderInSection
:(NSInteger
)section
{
return <#size#
>;
}
- (CGSize
)collectionView
:(UICollectionView
*)collectionView layout
:(UICollectionViewLayout
*)collectionViewLayout referenceSizeForFooterInSection
:(NSInteger
)section
{
return <#size#
>;
}
- (CGSize
)collectionView
:(UICollectionView
*)collectionView layout
:(UICollectionViewLayout
*)collectionViewLayout sizeForItemAtIndexPath
:(NSIndexPath
*)indexPath
{
CGFloat item_wh
= <#
float#
>;
return kSize(item_wh
, item_wh
);
}
-(UIEdgeInsets
)collectionView
:(UICollectionView
*)collectionView layout
:(UICollectionViewLayout
*)collectionViewLayout insetForSectionAtIndex
:(NSInteger
)section
{
return UIEdgeInsetsMake(0, 0, 0,0);
}
- (void)collectionView
:(UICollectionView
*)collectionView didSelectItemAtIndexPath
:(NSIndexPath
*)indexPath
{
[collectionView deselectItemAtIndexPath
:indexPath animated
:YES
];
[self collectionViewItemClickAction
:indexPath
];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning
];
}
#pragma mark - LazyLoad
- (UICollectionView
*)collectionView
{
if (!_collectionView
) {
UICollectionViewFlowLayout
*layout
= [UICollectionViewFlowLayout new
];
_collectionView
= [[UICollectionView alloc
]initWithFrame
:CGRectZero collectionViewLayout
:layout
];
_collectionView
.backgroundColor
= [UIColor clearColor
];
_collectionView
.delegate
= self;
_collectionView
.dataSource
= self;
[self.view addSubview
:_collectionView
];
if (@available(iOS
11.0,*)) {
_collectionView
.contentInsetAdjustmentBehavior
= UIScrollViewContentInsetAdjustmentNever
;
}else{
self.automaticallyAdjustsScrollViewInsets
= NO
;
}
_collectionView
.showsVerticalScrollIndicator
= NO
;
_collectionView
.frame
= kFrame(0,0, kIphone_W
, kIphone_H
- KNaviBarH
- kSystemGestureH
);
[_collectionView registerClass
:[UICollectionReusableView class
] forSupplementaryViewOfKind
:UICollectionElementKindSectionHeader withReuseIdentifier
:NSStringFromClass([UICollectionReusableView class
])];
[_collectionView registerClass
:[UICollectionReusableView class
] forSupplementaryViewOfKind
:UICollectionElementKindSectionFooter withReuseIdentifier
:NSStringFromClass([UICollectionReusableView class
])];
[_collectionView registerClass
:[UICollectionViewCell class
] forCellWithReuseIdentifier
:NSStringFromClass([UICollectionViewCell class
])];
}
return _collectionView
;
}
-(<#class#
> *)model
{
if (!_model
) {
_model
= [<#class#
> new
];
}
return _model
;
}
@end
至于新版Xcode10的代码片段怎么操作,可以参考这篇博客,这里不作赘述。
二、代码片段迁移
1、直接迁移文件夹
打开终端输入:
open ~/Library/Developer/Xcode/UserData/CodeSnippets
见下图:
如果没有CodeSnippets文件,那就说你没有自定义过自己的代码片段,也就不需要迁移代码的操作了。 如果有CodeSnippets文件,你可以直接拷贝这个文件夹,放到工作电脑的的相同位置。
2、远程仓库管理
这里只能提供个思路。可以将代码片段的提交至git仓库托管和脚本(后年说明),类似如下图:
在另一台电脑需要这些代码片段时,只需要将该文件下载下来,然后打开终端,跳转到该文件并执行脚本即可。 这种方式简单实用,甚至可以将它直接放到多人开发的项目中管理,实现代码片段的共享。 因为个人暂时不太懂脚本语言,不知道怎么实现。有哪个大神会的,还望能在评论该文的评论中上传哈?🤝 🤝 要么只能待以后有机会了解到这一块了,再更新脚本信息。
待更新~~