Swift Snippets
A collection of Swift snippets to be used in Xcode
Usage
- swifttrycatch
do {
try <#code#>
} catch <#errortype#> {
<#code#>
} catch <#errortype#> {
<#code#>
}
- swiftassociatedobject
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
)
}
}
}
- swiftavailable
@available(iOS 7, *)
- swiftcheckavailability
if #available(iOS 9, *) {
<#code#>
} else {
<#code>
}
- swiftcheckversion
#if swift(>=3.0)
<#code#>
#elseif swift(>=2.2)
<#code#>
#elseif swift(>=2.1)
<#code#>
#endif
- swiftcheckplatform
#if os(iOS) || os(tvOS)
<#code#>
#elseif os(watchOS)
<#code#>
#elseif os(OSX)
<#code#>
#endif
- swiftdispatchafter
DispatchQueue.main.asyncAfter(deadline: .now() + <#time#>) {
<#code#>
}
- swiftdispatchasync
DispatchQueue.global(qos: .background).async {
<#code#>
}
- swiftdispatchonce
let <#name#>: <#type#> = {
return <#code#>
}()
- swiftinitcoder
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
- swiftmark
// MARK: - <#section#>
- swiftsubscript
subscript(<#name#>: <#type#>) -> <#type#> {
get {
return <#value#>
}
set(newValue) {
<#code#>
}
}
- swifttypealias
typealias <#alias#> = <#existingtype#>
- swiftuitableviewdatasource
- swiftuicollectionviewdatasource
- swiftuipickerviewdatasource
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
Hyper Interaktiv AS, [email protected]
License
SwiftSnippets is available under the MIT license. See the LICENSE file for more info.