Skip to content

Instantly share code, notes, and snippets.

@ayanonagon
ayanonagon / StringExtension.swift
Created November 29, 2016 21:29
s_n_a_k_e_c_a_s_e_d
import Foundation
extension String {
func s_n_a_k_e_c_a_s_e_d() -> String { // OK, you can name it just “snakecased” if you prefer… :)
var charactersToRemove = CharacterSet.alphanumerics.inverted
charactersToRemove.remove(charactersIn: " ")
let result = components(separatedBy: charactersToRemove).joined(separator: "")
return result.replacingOccurrences(of: " ", with: "_").lowercased()
}
}
@ayanonagon
ayanonagon / CharacterSetCrash.swift
Created June 17, 2016 23:01
Swift 3.0 CharacterSet Bug
import Foundation
var nonAlphaNumeric = CharacterSet.alphanumerics.inverted
nonAlphaNumeric.remove(charactersIn: " ")
import UIKit
protocol Coordinator {
func start()
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
struct Item {
let name: String
var price: UInt
init(name: String, price: UInt) {
self.name = name
self.price = price
}
}
class Cart {
let items: [Item]
var total: UInt {
return items.reduce(0) { sum, item in
sum + item.price
}
}
init(items: [Item]) {
class Item {
let name: String
var price: UInt
init(name: String, price: UInt) {
self.name = name
self.price = price
}
}
func listUsers(completion: Result<[User]> -> Void) {
// Get the list of users
// If success, call completion(Result.Success(users))
// If error, call completion(Result.Error(error))
}
enum Result<T> {
case Success(T)
case Error(NSError)
}
func listMessages(completion: Result<[Message]> -> Void) {
// Get the list of messages
// If success, call completion(Result.Success(messages))
// If error, call completion(Result.Error(error))
}
func listMessages(completion: Result -> Void) {
// Get the list of messages
// If success, call completion(.Success(messages: messages))
// If error, call completion(.Error(error: error))
}
enum Result {
case Success(messages: [Message])
case Error(error: NSError)
}