Skip to content

Latest commit

 

History

History

swift

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Swift

This guide is built to share the best practices in writing swift code.

Fundamentals

  • Use english to code.
  • Use camel case. For example:
      let isCorrect: Bool
  • Use descriptive code, clarity is more important than brevity. For example:
      let isFriday: Bool {}
      if isFriday {
        print("Friday!!!!")
      }

Swift style guide

Swiftlint

  disabled_rules:
  - control_statement
  - line_length
  - trailing_whitespace
  - force_cast
  - class_delegate_protocol
  - weak_delegate

  opt_in_rules:
  - empty_count

  excluded:
  - Pods

  vertical_whitespace:
  max_empty_lines: 2

  file_length:
  warning: 500
  error: 1000

IOS

IOS App Architecture

There are many architectures developers use to build IOS apps, MVC (Model View Controller), is Apple recommended architecture pattern.

Api Client Setup

Many IOS applications use external Api, this is a setup which might come useful.

  • This setup uses a singleton class, ApiClient in which the api methods are written.
  class ApiClient {
      static let shared = ApiClient()
    ...
  }
  • Its also useful to divide the url endpoints to the base url
  enum Endpoints {
  case .getNames
   ...  
  }
  • The methods use completion handlers. Something like this:
   func getNames(completion: @escaping (Result(T))) {
     ...
     if error != nil {
         completion(.error)
     } else {
       completion(.success(names))
     }
   }

Building views form xib's

Building views from xib's can be useful, to do this we recommend following steps shown in this post, Loading views from XIB’s on iOS, from Icalia Labs blog by Daniel Lozano.

Project structure for MVC

  • As for folder structure, follow something like this:
    • Models
    • Views
    • Controllers
    • Others

Pod installations

Swift Packet Manager

  • Swift Packet Manager is used to create swift libraries and and command line apps in macOS.
  mkdir CommandLineApp
  cd CommandLineApp
  swift package init --type executable