【iOS】代码片段汇总与迁移管理

简介: 【iOS】代码片段汇总与迁移管理

以下是个人长期习惯使用的代码片段,是承受了个人的代码习惯的多次演变的而总结出来的。

正所谓飘飘树叶千千万,也找不出两片是一样的,但是可以找到我们认为最美的那片叶子。同样,每个人的编码风格都会有所不懂,都希望找出自己所认为的简单明了的最方便的编码风格。

没有长期固定的样式,只有在不断的优化中寻找真我。

下面就是代码片段的截图样式,标题的前后两个分别标注:title(标题)、Completion Shortcut(快捷方式)

2019100515381141.png


一、常用代码片段

1、assign - @assign


///<#description#>
@property (nonatomic, assign) <#class#> <#name#>;

2、copy - @copy

 

///<#description#>
    @property (nonatomic, copy) <#class#> *<#name#>;


3、strong - @strong

///<#description#>
@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

//Views
#import "ZM_YYLabel_TVCell.h"
// model
#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

//Views
#import "<#CVCellClass#>.h"
//Models
#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{
    //cell
    [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);
}
/单元格x间的间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
//    return 0;
//}
/单元格y间的间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
//    return 0;
//}
///点击单元格方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    [self collectionViewItemClickAction:indexPath];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#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])];
        //cell
        [_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

见下图:

20200604233636702.png


如果没有CodeSnippets文件,那就说你没有自定义过自己的代码片段,也就不需要迁移代码的操作了。

如果有CodeSnippets文件,你可以直接拷贝这个文件夹,放到工作电脑的的相同位置。


2、远程仓库管理

这里只能提供个思路。可以将代码片段的提交至git仓库托管和脚本(后年说明),类似如下图:

20200604232239717.png


在另一台电脑需要这些代码片段时,只需要将该文件下载下来,然后打开终端,跳转到该文件并执行脚本即可。

这种方式简单实用,甚至可以将它直接放到多人开发的项目中管理,实现代码片段的共享。

因为个人暂时不太懂脚本语言,不知道怎么实现。有哪个大神会的,还望能在评论该文的评论中上传哈?🤝 🤝

要么只能待以后有机会了解到这一块了,再更新脚本信息。


待更新~~


相关文章
|
网络协议 API iOS开发
快速零配置迁移 API 适配 iOS 对 IPv6 以及 HTTPS 的要求
本文快速分享一下快速零配置迁移 API 适配 iOS 对 IPv6 以及 HTTPS 的要求的方法,供大家参考。原文发表于我的技术博客 零配置方案 最新的苹果审核政策对 API 的 IPv6 以及 HTTPS 都作了要求,那么如何快速进行适配呢? 这里就快速给大家分享一个站点:https://www.cloudflare.com/ 注意它其中有两个重要的功能,进行配置,迁移 DNS 即可,详细的使用请自行查阅文档或者直接在小密圈给我提问即可。
1046 0
|
存储 缓存 数据库
ios开发数据库版本迁移手动更新迭代和自动更新迭代艺术(-)
demo 地址 https://github.com/PureLovePeter/DataCache 好用的话 star star star 数据库版本迁移顾名思义就是在原有的数据库中更新数据库,数据库中的数据保持不变对表的增、删、该、查。
1023 0
|
存储 数据库 iOS开发
ios开发数据库版本迁移手动更新迭代和自动更新迭代艺术(二)
由于大家都热衷于对ios开发数据库版本迁移手动更新迭代和自动更新迭代艺术(一)的浏览下面我分享下我的源文件git仓库:     用法(这边我是对缓存的一些操作不需要可以省去):https://github.
|
iOS开发
微信ios版6.2更新 聊天记录迁移更快捷朋友圈可翻译
  微信iPhone版昨日发布更新,版本号为微信 6.2 for iOS,主要特性有聊天记录迁移、发送图片更快捷、支持朋友圈翻译、手机充值可充流量查余额、可以通过展示二维码来收钱,和ytkah一起来瞧瞧吧   聊天记录可以快速导入到新手机,不用担心换手机后聊天记录的遗失。
980 0
|
4天前
|
JavaScript 搜索推荐 Android开发
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
23 8
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
|
1月前
|
iOS开发 开发者
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
143 67
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
|
2月前
|
存储 监控 API
app开发之安卓Android+苹果ios打包所有权限对应解释列表【长期更新】-以及默认打包自动添加权限列表和简化后的基本打包权限列表以uniapp为例-优雅草央千澈
app开发之安卓Android+苹果ios打包所有权限对应解释列表【长期更新】-以及默认打包自动添加权限列表和简化后的基本打包权限列表以uniapp为例-优雅草央千澈
90 11
|
2月前
|
iOS开发 开发者 MacOS
深入探索iOS开发中的SwiftUI框架
【10月更文挑战第21天】 本文将带领读者深入了解Apple最新推出的SwiftUI框架,这一革命性的用户界面构建工具为iOS开发者提供了一种声明式、高效且直观的方式来创建复杂的用户界面。通过分析SwiftUI的核心概念、主要特性以及在实际项目中的应用示例,我们将展示如何利用SwiftUI简化UI代码,提高开发效率,并保持应用程序的高性能和响应性。无论你是iOS开发的新手还是有经验的开发者,本文都将为你提供宝贵的见解和实用的指导。
145 66
|
2月前
|
开发框架 Android开发 iOS开发
安卓与iOS开发中的跨平台策略:一次编码,多平台部署
在移动应用开发的广阔天地中,安卓和iOS两大阵营各占一方。随着技术的发展,跨平台开发框架应运而生,它们承诺着“一次编码,到处运行”的便捷。本文将深入探讨跨平台开发的现状、挑战以及未来趋势,同时通过代码示例揭示跨平台工具的实际运用。
173 3

热门文章

最新文章

  • 1
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 2
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 3
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
  • 4
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
  • 5
    【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
  • 6
    iOS8 中无需开源库的内置功能一览
  • 7
    iOS7应用开发7:自定义视图、手势操作
  • 8
    IOS小工具以及精彩的博客
  • 9
    Facebook SDK(iOS)初学讲解
  • 10
    iOS - Swift NSPoint 位置
  • 1
    【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
    14
  • 2
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
    28
  • 3
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    34
  • 4
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    29
  • 5
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
    23
  • 6
    uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
    143
  • 7
    【05】2025年1月首发完整版-篇幅较长-苹果app如何上架到app store完整流程·不借助第三方上架工具的情况下无需花钱但需仔细学习-优雅草央千澈详解关于APP签名以及分发-们最关心的一篇来了-IOS上架app
    235
  • 8
    app开发之安卓Android+苹果ios打包所有权限对应解释列表【长期更新】-以及默认打包自动添加权限列表和简化后的基本打包权限列表以uniapp为例-优雅草央千澈
    90
  • 9
    深入探索iOS开发中的SwiftUI框架
    145
  • 10
    ios样式开关按钮jQuery插件
    60