SwiftLint——Swift代码检查及自动格式化工具
某软不给力,正在做的UWP项目停工了。官方说法是要等到RS2发布新的VOIP架构,再看看是不是给某软面子。虽然Beta用户中发出了几点愤怒的声音,但是木有用。有用的只能是某软的Skype for business UWP版拿下几个大订单,才有说服力。像现在这样鶸的表现,真是让人心寒……
当然UWP开发入门还会继续写下去,本篇只是偷个懒,把工作中整理的资料放上来。蜀黍我可不是叛徒,我没发过誓不给水果开发APP,某软现在就是最大的果蛆。无奈混口饭吃,让写Swift就写呗,水果一套加上Xcode,咋用咋不爽,跟太阳系最强IDE比实在差距有点大,不得已还得额外安装代码格式检查的工具,安装过程也是学习使用MacOS,Terminal一系列陌生玩意的痛苦经历,风格迥异让我生不如死。特地写此一篇备忘,及供和我一样的iOS鶸参考。
What can SwiftLint do?
- Give code style warnings and errors
- Auto format swift code file

Installation
- You can also install SwiftLint by downloading SwiftLint.pkg from the latest GitHub release and running it.
Integrate SwiftLint into an Xcode scheme to get warnings and errors displayed in the IDE.
In project file, just add a new "Run Script Phase" with:
if which swiftlint >/dev/null; then
swiftlint
else
echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi

Warnings and errors
Build project , swiftlint will display warnings and errors in Xcode.

We can disable or create some rules in configuration file.
Swiftlint Auto Correct
For formatting code files, you need to input "swiftlint autocorrect" in terminal.
cd documents/projects/MyProject/SomeFolder
swiftlint autocorrect
If you want to lint only one file:
cd documents/projects/MyProject/SomeFolder
swiftlint autocorrect --path SampleCode.swift
Unformatted:

Formatted:

Download Swift Configuration File
You can download swift configuration file in any folder. But the file which name starts with "." will be hidden on MacOS.
So I suggest you create an new file ".swiftlint.yml" in Xcode, under your project root folder.
Input below in terminal
cd documents/projects/yourProjectName
curl https://raw.githubusercontent.com/kevindelord/swift-style-guide/master/.swiftlint.yml > .swiftlint.yml
Press enter button and download. Then you can get lateset .swiftlint.yml and edit in Xcode.

Disable, included or create custom rule in .swiftlint.yml file
- Disable rules
Add "- rule name" below "disabled_rules:"
disabled_rules:
- trailing_whitespace
- Included folder
Only check child foder with SwiftLint
included:
- MyProject/NeedCheckFolder
- Create custom rules
Use Regular Expression (regex) create custom rules
custom_rules:
comments_space:
name: "Space After Comment"
regex: "(^ *//\w+)"
message: "There should be a space after //"
severity: error
multiple_empty_lines:
name: "Multiple Empty Lines"
regex: ".\n(\s*\n){2,}"
message: "There are too many line breaks"
References
https://github.com/realm/SwiftLint
http://kevindelord.io/2016/04/06/integrate-swiftlint/
https://swifting.io/blog/2016/03/29/11-swiftlint/
SwiftLint——Swift代码检查及自动格式化工具的更多相关文章
- swiftlint swift代码规范检查神器
一大堆的你为什么要用swiftlint,你为什么要codereview,swiftlint到底在做什么,就不多说了,没意义,他就是帮助你写出规范漂亮代码的神器! 安装 官方提供了三种安装的方式 hom ...
- SwiftLint:代码规范检查工具介绍
Swift-CodeStyle Checker:SwiftLint 介绍: SwiftLint 是一个用于强制检查 Swift 代码风格和规定的一个工具,基本上以 GitHub's Swift 代码风 ...
- 使用Xcode HeaderDoc和Doxygen文档化你的Objective-C和Swift代码
在一个应用的整个开发过程中涉及到了无数的步骤.其中一些是应用的说明,图片的创作,应用的实现,和实现过后的测试阶段.写代码可能组成了这个过程的绝大部分,因为正是它给了应用生命,但是这样还不够,与它同等重 ...
- [原创]Java静态代码检查工具介绍
[原创]Java静态代码检查工具介绍 一 什么是静态代码检查? 静态代码分析是指无需运行被测代码,仅通过分析或检查源程序的语法.结构.过程.接口等来检查程序的正确性,找出代码隐藏的错误和缺陷,如参数 ...
- Cocos2D-ObjC:在RPG游戏中混合Swift代码
我之前写过一个RPG游戏<<熊猫之魂 SoulOfPanda>> 编译器使用的是SpriteBuilder,很好很强大!全部代码都由Objc完成,现在想尝试一下在其中混入Swi ...
- 在Jenkins中使用sonar进行静态代码检查
要解决的问题 jenkins自动构建完成后,希望能通过sonar静态代码检查生成一份报告,给与开发人员对当前代码的做一个质量评估和修改意见 1.安装并配置sonar服务器 懒得说,跟着官方文档走就行, ...
- CSS代码检查工具stylelint
前面的话 CSS不能算是严格意义的编程语言,但是在前端体系中却不能小觑. CSS 是以描述为主的样式表,如果描述得混乱.没有规则,对于其他开发者一定是一个定时炸弹,特别是有强迫症的人群.CSS 看似简 ...
- C#静态代码检查工具StyleCode
C#静态代码检查工具StyleCode -- 初探 最近我们Advent Data Service (ADS) 在项目上需要按照代码规范进行代码的编写工作,以方便将来代码的阅读与维护. 但是人工检查起 ...
- 在 OC 中调用 Swift 代码
1.在 Objective-C 项目中使用 Swift 代码 1)在 OC 项目中创建 .Swift 文件,文件中的格式为其本有的格式. 2)Xcode 提示是否创建 Objective-C brid ...
随机推荐
- 精神哥讲Crash(一):UnsatisfiedLinkError
版权声明:本文为腾讯Bugly原创文章,如需转载,请标明出处. 大家好,我是腾讯Bugly的精神哥(英文名:spirit),是Bugly资深码奴的同时,又是Bugly神秘的Crash实验室研究员哦 ...
- jquery插件——点击交换元素位置(带动画效果)
一.需求的诞生 在我们的网页或者web应用中,想要对列表中的元素进行位置调整(或者说排序)是一个常见的需求.实现方式大概就以下两种,一种是带有类似“上移”.“下移”的按钮,点击可与相邻元素交换位置,另 ...
- ubuntu 13.04下MYSQL 5.5环境搭建
解决的问题: 安装mysql server和mysql client 5.5 新建远程账户 远程访问权限 MYSQL默认字符集修改为UTF8 检查防火墙 一.安装 BTW:可以使用查找命令查看安装包 ...
- iOS——Core Animation 知识摘抄(三)
原文地址:http://www.cocoachina.com/ios/20150105/10827.html CAShapeLayer CAShapeLayer是一个通过矢量图形而不是bitmap来绘 ...
- Lock,LockFree,MemoryBarrier,ConcurrentCollection
最近看并行编程书本的一些心得,简单记录下多线程和并行编程必知必会的几个概念,再次加深自己的理解. .NET Framework4提供了一个新的命名空间System.Collections.Concur ...
- SiteMesh3整合SpringMVC+FreeMarker
SiteMesh3配置 添加maven依赖 添加filter 配置servlet 添加sitemesh配置文件 decorator示例 SpringMVC.FreeMarker配置(404问题处理) ...
- Web开发人员必读的12个网站
The more you actually create, the more you’ll learn.(创造的越多,学习的越多),世界上有无数个开发人员会在网上分享他们的开发经验,我们无法向所有人学 ...
- Lucene系列-搜索
Lucene搜索的时候就要构造查询语句,本篇就介绍下各种Query.IndexSearcher是搜索主类,提供的常用查询接口有: TopDocs search(Query query, int n); ...
- 每天一个linux命令(53):route命令
Linux系统的route命令用于显示和操作IP路由表(show / manipulate the IP routing table).要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或 ...
- [Spring框架]Spring AOP基础入门总结一.
前言:前面已经有两篇文章讲了Spring IOC/DI 以及 使用xml和注解两种方法开发的案例, 下面就来梳理一下Spring的另一核心AOP. 一, 什么是AOP 在软件业,AOP为Aspect ...