Home

Awesome

Swift Snippets

❤️ Support my app ❤️

❤️❤️😇😍🤘❤️❤️

A collection of Swift snippets to be used in Xcode

Usage

do {
  try <#code#>
} catch <#errortype#> {
  <#code#>
} catch <#errortype#> {
  <#code#>
}
private struct AssociatedKeys {
  static var <#name#> = "<#name#>"
}

var <#name#>: String? {
  get {
    return objc_getAssociatedObject(self, &AssociatedKeys.<#name#>) as? String
  }

  set {
    if let newValue = newValue {
      objc_setAssociatedObject(self, &AssociatedKeys.<#name#>, newValue as String?,
        .OBJC_ASSOCIATION_RETAIN_NONATOMIC
      )
    }
  }
}
@available(iOS 7, *)
if #available(iOS 9, *) {
    <#API available statements#>
} else if #available(macOS 10.12, *) {
    <#API available statements#>
} else {
    <#fallback statements#>
}
#if swift(>=3.0)
    <#code#>
#elseif swift(>=2.2)
    <#code#>
#elseif swift(>=2.1)
    <#code#>
#endif
#if os(iOS) || os(tvOS)
  <#code#>
#elseif os(watchOS)
  <#code#>
#elseif os(OSX)
  <#code#>
#endif
DispatchQueue.main.asyncAfter(deadline: .now() + <#time#>) {
  <#code#>
}
DispatchQueue.global(qos: .background).async {
  <#code#>
}
let <#name#>: <#type#> = {
  return <#code#>
}()
public required init?(coder aDecoder: NSCoder) {
  fatalError("init(coder:) has not been implemented")
}
// MARK: - <#section#>
subscript(<#name#>: <#type#>) -> <#type#> {
  get {
    return <#value#>
  }
  set(newValue) {
      <#code#>
  }
}
guard let `self` = self else {
  return
}
func numberOfSections(in tableView: UITableView) -> Int {
 return <#count#>
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
 return <#count#>
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 <#code#>
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
  return <#count#>
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  return <#count#>
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  <#code#>
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
  return <#count#>
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  return <#count#>
}

Installation

Manual

Drag codesnippet files from Snippets into /Library/Developer/Xcode/UserData/CodeSnippets

Automatic

Run this in your terminal

curl -fsSL https://raw.githubusercontent.com/hyperoslo/SwiftSnippets/master/install.sh | sh

Author

Credit

License

SwiftSnippets is available under the MIT license. See the LICENSE file for more info.