{"id":2755,"date":"2021-06-19T15:11:33","date_gmt":"2021-06-19T11:11:33","guid":{"rendered":"https:\/\/blog.alwawee.ru\/?p=2755"},"modified":"2021-07-04T12:12:26","modified_gmt":"2021-07-04T08:12:26","slug":"how-to-add-the-swiftlint-without-pain","status":"publish","type":"post","link":"https:\/\/blog.alwawee.ru\/?p=2755","title":{"rendered":"How to add the SwiftLint without pain"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Why do you need SwiftLint?<\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li>It speed ups code reviews, you stop wasting time on code formatting, force casting, and other common issues<\/li><li>It improves the code quality, code becomes more readable, and maintainable<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"> <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Integration<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. Add SwiftLint to a podfile<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Or use another option from the <a href=\"https:\/\/github.com\/realm\/SwiftLint\">official documentation<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  # A tool to enforce Swift style and conventions.\n  pod 'SwiftLint', '0.43.1'<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2. Add this .swiftlint.yml config file to the project root folder<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The main trick to make integration easy is to disable all rules at first.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>disabled_rules:\n- trailing_whitespace\n- force_cast\n- force_unwrapping\n- force_try\n- empty_string\n- line_length\n- mark\n- weak_delegate\n- unneeded_break_in_switch\n- identifier_name\n- function_parameter_count\n- unused_closure_parameter\n- vertical_whitespace\n- opening_brace\n- control_statement\n- statement_position\n- comma\n- colon\n- trailing_comma\n\nopt_in_rules:\n- empty_count\n- empty_string\n\nexcluded:\n- Carthage\n- Pods\n- SwiftLint\/Common\/3rdPartyLib\n\nline_length:\n    warning: 150\n    error: 200\n    ignores_function_declarations: true\n    ignores_comments: true\n    ignores_urls: true\n\nfunction_body_length:\n    warning: 300\n    error: 500\n\nfunction_parameter_count:\n    warning: 6\n    error: 8\n\ntype_body_length:\n    warning: 300\n    error: 500\n\nfile_length:\n    warning: 1000\n    error: 1500\n    ignore_comment_only_lines: true\n\ncyclomatic_complexity:\n    warning: 15\n    error: 25\n\nreporter: \"xcode\"<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3. Build a project, add additional rules to the disabled_rules section, if there are many warnings for them<\/h4>\n\n\n\n<h4 class=\"wp-block-heading\">4. Delete rules from the disabled_rules section one by one and fix all the warnings<\/h4>\n\n\n\n<h4 class=\"wp-block-heading\">5. Leave the trailing_whitespace and other rules that you don&#8217;t want to fix in the disabled_rules section<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">In the end, you will have something like this in your config file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>disabled_rules:\n- trailing_whitespace\n\nopt_in_rules:\n- empty_count\n- empty_string\n\nexcluded:\n- Carthage\n- Pods\n- SwiftLint\/Common\/3rdPartyLib\n\nline_length:\n    warning: 150\n    error: 200\n    ignores_function_declarations: true\n    ignores_comments: true\n    ignores_urls: true\n\nfunction_body_length:\n    warning: 300\n    error: 500\n\nfunction_parameter_count:\n    warning: 6\n    error: 8\n\ntype_body_length:\n    warning: 300\n    error: 500\n\nfile_length:\n    warning: 1000\n    error: 1500\n    ignore_comment_only_lines: true\n\ncyclomatic_complexity:\n    warning: 15\n    error: 25\n\nreporter: \"xcode\"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Some ideas on how to fix common code issues<br><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Force Cast Violation: Force casts should be avoided. (force_cast)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Problem:<\/p>\n\n\n<pre lang=\"swift\">let cell = collectionView.dequeueReusableCell(\n    withReuseIdentifier: \"DocumentCollectionViewCell\",\n    for: indexPath\n) as! DocumentCollectionViewCell\n<\/pre>\n\n\n<p class=\"wp-block-paragraph\">Solution:<\/p>\n\n\n<pre lang=\"swift\">guard let cell = collectionView.dequeueReusableCell(\n    withReuseIdentifier: \"DocumentCollectionViewCell\",\n    for: indexPath\n) as? DocumentCollectionViewCell else { fatalError() }\n<\/pre>\n\n\n<h4 class=\"wp-block-heading\">Force Try Violation: Force tries should be avoided. (force_try)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Problem:<\/p>\n\n\n<pre lang=\"swift\">let workingDirectory = try! FileManager.default.url(\n    for: .documentDirectory,\n    in: .userDomainMask,\n    appropriateFor: nil,\n    create: false\n)\n<\/pre>\n\n\n<p class=\"wp-block-paragraph\">Solution:<\/p>\n\n\n<pre lang=\"swift\">do {\n    let workingDirectory = try FileManager.default.url(\n        for: .documentDirectory,\n        in: .userDomainMask,\n        appropriateFor: nil,\n        create: false\n    )\n} catch {\n    print(error)\n}\n<\/pre>\n\n\n<h4 class=\"wp-block-heading\">Exceptions <\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">In very rare cases you will not be able to fix the warning. For example, when you use a framework and you cannot easily change its API. Then you can switch off a SwiftLint rule for a particular code block using <em>\/\/ swiftlint:disable rule_name<\/em> and <em>\/\/ swiftlint:enable rule_name<\/em> comments. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Why do you need SwiftLint? It speed ups code reviews, you stop wasting time on code formatting, force casting, and other common issues It improves the code quality, code becomes more readable, and maintainable Integration 1. Add SwiftLint to a podfile Or use another option from the official documentation. 2. Add this .swiftlint.yml config file [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[199,213],"class_list":["post-2755","post","type-post","status-publish","format-standard","hentry","category-ios","tag-swift","tag-swiftlint"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to add the SwiftLint without pain - Denis Kutlubaev<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.alwawee.ru\/?p=2755\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to add the SwiftLint without pain - Denis Kutlubaev\" \/>\n<meta property=\"og:description\" content=\"Why do you need SwiftLint? It speed ups code reviews, you stop wasting time on code formatting, force casting, and other common issues It improves the code quality, code becomes more readable, and maintainable Integration 1. Add SwiftLint to a podfile Or use another option from the official documentation. 2. Add this .swiftlint.yml config file [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.alwawee.ru\/?p=2755\" \/>\n<meta property=\"og:site_name\" content=\"Denis Kutlubaev\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/alwaweemobile\" \/>\n<meta property=\"article:published_time\" content=\"2021-06-19T11:11:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-07-04T08:12:26+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/blog.alwawee.com\/?attachment_id=1982\" \/>\n<meta name=\"author\" content=\"Denis Kutlubaev\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Denis Kutlubaev\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2755#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2755\"},\"author\":{\"name\":\"Denis Kutlubaev\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"headline\":\"How to add the SwiftLint without pain\",\"datePublished\":\"2021-06-19T11:11:33+00:00\",\"dateModified\":\"2021-07-04T08:12:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2755\"},\"wordCount\":230,\"commentCount\":0,\"keywords\":[\"Swift\",\"SwiftLint\"],\"articleSection\":[\"iOS\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2755#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2755\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2755\",\"name\":\"How to add the SwiftLint without pain - Denis Kutlubaev\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\"},\"datePublished\":\"2021-06-19T11:11:33+00:00\",\"dateModified\":\"2021-07-04T08:12:26+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2755#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2755\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2755#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/blog.alwawee.ru\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to add the SwiftLint without pain\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/\",\"name\":\"Denis Kutlubaev\",\"description\":\"iOS Development\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blog.alwawee.ru\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\",\"name\":\"Denis Kutlubaev\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"caption\":\"Denis Kutlubaev\"},\"description\":\"iOS Developer, creator of Tornado Browser and many other popular apps\",\"sameAs\":[\"http:\\\/\\\/dennis.alwawee.com\"],\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to add the SwiftLint without pain - Denis Kutlubaev","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.alwawee.ru\/?p=2755","og_locale":"en_US","og_type":"article","og_title":"How to add the SwiftLint without pain - Denis Kutlubaev","og_description":"Why do you need SwiftLint? It speed ups code reviews, you stop wasting time on code formatting, force casting, and other common issues It improves the code quality, code becomes more readable, and maintainable Integration 1. Add SwiftLint to a podfile Or use another option from the official documentation. 2. Add this .swiftlint.yml config file [&hellip;]","og_url":"https:\/\/blog.alwawee.ru\/?p=2755","og_site_name":"Denis Kutlubaev","article_publisher":"https:\/\/www.facebook.com\/alwaweemobile","article_published_time":"2021-06-19T11:11:33+00:00","article_modified_time":"2021-07-04T08:12:26+00:00","og_image":[{"url":"http:\/\/blog.alwawee.com\/?attachment_id=1982","type":"","width":"","height":""}],"author":"Denis Kutlubaev","twitter_misc":{"Written by":"Denis Kutlubaev","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.alwawee.ru\/?p=2755#article","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/?p=2755"},"author":{"name":"Denis Kutlubaev","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"headline":"How to add the SwiftLint without pain","datePublished":"2021-06-19T11:11:33+00:00","dateModified":"2021-07-04T08:12:26+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.alwawee.ru\/?p=2755"},"wordCount":230,"commentCount":0,"keywords":["Swift","SwiftLint"],"articleSection":["iOS"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blog.alwawee.ru\/?p=2755#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blog.alwawee.ru\/?p=2755","url":"https:\/\/blog.alwawee.ru\/?p=2755","name":"How to add the SwiftLint without pain - Denis Kutlubaev","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/#website"},"datePublished":"2021-06-19T11:11:33+00:00","dateModified":"2021-07-04T08:12:26+00:00","author":{"@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"breadcrumb":{"@id":"https:\/\/blog.alwawee.ru\/?p=2755#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.alwawee.ru\/?p=2755"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.alwawee.ru\/?p=2755#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.alwawee.ru\/"},{"@type":"ListItem","position":2,"name":"How to add the SwiftLint without pain"}]},{"@type":"WebSite","@id":"https:\/\/blog.alwawee.ru\/#website","url":"https:\/\/blog.alwawee.ru\/","name":"Denis Kutlubaev","description":"iOS Development","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.alwawee.ru\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7","name":"Denis Kutlubaev","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","caption":"Denis Kutlubaev"},"description":"iOS Developer, creator of Tornado Browser and many other popular apps","sameAs":["http:\/\/dennis.alwawee.com"],"url":"https:\/\/blog.alwawee.ru\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/2755","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2755"}],"version-history":[{"count":18,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/2755\/revisions"}],"predecessor-version":[{"id":2826,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/2755\/revisions\/2826"}],"wp:attachment":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2755"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2755"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2755"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}