defaults write com.apple.dt.Xcode IDEIndexerActivityShowNumericProgress -bool true
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class VC: UIViewController { | |
func formatter() -> DateFormatter { | |
struct Formatter { | |
static let f: DateFormatter = { | |
let f = DateFormatter() | |
f.dateStyle = .medium | |
print("I am called just once") | |
return f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct Person: Codable { | |
public let name: String | |
} | |
let person = Person(name: "Bas") | |
let payload = try JSONEncoder().encode(person) | |
let string = String(data: payload, encoding: .utf8) // {"name":"Bas"} | |
let decodedPerson = try JSONDecoder().decode(Person.self, from: payload) // Person(name: "Bas") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension Sequence where Element: Equatable { | |
/// Returns a Boolean value indicating whether every element of the sequence | |
/// is equal to the given element. | |
func containsOnly(_ element: Element) -> Bool { | |
var iterator = self.makeIterator() | |
guard iterator.next() != nil else { return false } | |
return first(where: { $0 != element }) == nil |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if swift(>=4.1) | |
#else | |
extension Collection { | |
func compactMap<ElementOfResult>( | |
_ transform: (Element) throws -> ElementOfResult? | |
) rethrows -> [ElementOfResult] { | |
return try flatMap(transform) | |
} | |
} | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct A: Hashable { } | |
struct B: Hashable { // error: type 'B' does not conform to protocol 'Hashable' | |
let aa: [A] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Beware: this is "pseudocode", which is not guaranteed to compile. ;-) | |
import CoreData | |
protocol CoreDataProtocol { | |
var managedObjectContext: NSManagedObjectContext? { get } | |
} | |
class Card: NSManagedObject, CoreDataProtocol { | |
// conforms by default |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Solution<Problem>: Collection { | |
struct Step<T> { | |
let step: T | |
} | |
let steps: [Step<Problem>] | |
var input: Step<Problem> { | |
return steps.first! // safe, as steps will guaranteed to be larger than 0. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Vector<T> { | |
let size: Int | |
let initialValue: T | |
struct Position: Equatable { | |
let row: Int | |
let column: Int | |
} | |
typealias Matrix = [Position] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct Messages: Decodable { | |
let count: String | |
let messages: [Message] | |
enum CodingKeys: String, CodingKey { | |
case count = "message-count" | |
case messages | |
} |