Skip to content

Instantly share code, notes, and snippets.


Simon C. Krüger kayoslab

View GitHub Profile
kayoslab / UserDefaults.swift
Created Jul 8, 2020
A Property wrapper around UserDefaults which makes it easier to interact with.
View UserDefaults.swift
import Foundation
let defaults = UserDefaults(suiteName: "GistApplication")
/// A Property wrapper around UserDefaults which makes it easier to interact with.
@propertyWrapper struct UserDefault<T> {
/// The key of the entry which should be added to the defaults.
let key: String
kayoslab / Collection+Safe.swift
Created Jul 8, 2020
A safe way to subscribe collection items. This should put a definite end to index-out-of-bounds errors.
View Collection+Safe.swift
import Foundation
extension Collection {
/// Safe subscription for collection elements.
/// - Parameter safe: The index of which the element should be returned.
/// - Returns: The element at the given index if present, otherwise nil.
subscript (safe index: Index) -> Element? {
return indices.contains(index) ? self[index] : nil
kayoslab / UIView+nib.swift
Last active Jul 8, 2020
A UIView extension to make it easier working with nib and code layouts at the same time. This usually is present in every project I'm working on the interface.
View UIView+nib.swift
import UIKit
/// A UIView extension to make it easier working with nib and code layouts at the same time.
public extension UIView {
/// Unarchives the contents of a nib file.
/// - Returns: The top-level objects in the nib file.
class func fromNib<T: UIView>() -> T {
return Bundle(for: T.self).loadNibNamed(
kayoslab / .gitignore
Created Jul 8, 2020
A useful .gitignore file for iOS development
View .gitignore
# Xcode
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
## Build generated
## Various settings
kayoslab / swiftlint.yml
Created Jul 8, 2020
Default configuration for SwiftLint. This is the standard configuration I use in most of my projects.
View swiftlint.yml
### Global configuration
# Enable rules which are not in the default set
# - anyobject_protocol # Prefer using `AnyObject` over `class` for class-only protocols.
# - array_init # Prefer using `Array(seq)` over ` { $0 }` to convert a sequence into an Array.
# - attributes # Attributes should be on their own lines in functions and types, but on the same line as variables and imports.
- block_based_kvo # Prefer the new block based KVO API with keypaths when using Swift 3.2 or later.
- class_delegate_protocol # Delegate protocols should be class-only so they can be weakly referenced.
- closing_brace # Closing brace with closing parenthesis should not have any whitespaces in the middle.
kayoslab / AlipayOrder.swift
Last active Sep 19, 2018
Swift port according to the documentation on the Alipay website. I should create an example project for this in the future. As already mentioned in the legacy version of this class I'm relying on SwiftyRSA for the signing here.
View AlipayOrder.swift
import Foundation
import SwiftyRSA
/// Automatically generated by the Chinese Website.
struct AlipayPublicParameter {
enum CodingKeys: String, CodingKey {
case appID = "app_id"
case method
case format
case charset
kayoslab / LegacyAlipayOrder.swift
Created Sep 18, 2018
Swift port of the Legacy Alipay Order, which is usually only available in Objective-C. I rely on the RSA implementation of SwiftyRSA here, so that I don't have to work with their own (obj-c) implementations.
View LegacyAlipayOrder.swift
import Foundation
import SwiftyRSA
/// Swift port of the always included _Order_ class from Alipay, because we suffered from the terrible code.
/// The code comments went through google translate to give us some hints on what is happening under the hood.
/// This struct is used to hold the data for an order within the alipay framework and to get the url parameter
/// containing the order object including a RSA signature.
private struct LegacyAlipayOrder {
/********************************** Pay four elements **********************************/
/// When the merchant signs an agreement with Alipay, Alipay assigns a unique
kayoslab / NotificationService.swift
Created Jul 26, 2018
Example implementation for a basic iOS 10 UNNotificationServiceExtension to handle jpg/gif/png files
View NotificationService.swift
import UserNotifications
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
self.bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
kayoslab / Logging.swift
Last active Sep 7, 2017
Quick test case implementation of a logging manager
View Logging.swift
class Logging {
/// An Enum that describes the actual Level on which
/// Messages should be logged to the console.
public enum Level: Int {
/// Log all messages.
case verbose = 0
/// Log messages of information level and higher.
case information = 1
/// Log messages of warning level and higher.
case warning = 2
kayoslab / QueueHandler.swift
Created Sep 5, 2017
A simple Generic Queue with different priority Levels
View QueueHandler.swift
import Foundation
/// Different Priority Levels for Queue Objects
public enum QueuePriority: Int {
case low = 0
case medium = 1
case high = 2
/// Returns the priority Type for using the main Queue.
fileprivate static func mainQueuePriority() -> QueuePriority {