Skip to content

Instantly share code, notes, and snippets.

struct Bar {
init<T : LosslessStringConvertible>(_ f: T) {}
init<T>(anything: T) {}
}
let b = Bar(UnicodeScalar(4))
func test(closure: () -> Void) -> () -> () {
let localClosure : @noescape () -> Void = { closure() }
localClosure()
return localClosure // Invalid conversion from non-escaping function of type () -> Void to potentially escaping function type () -> ()
}
func test(closure: () -> Void) {
let localClosure : @noescape () -> Void = { closure() }
localClosure()
}
class Foo {}
class Bar : Foo {}
class Baz : Foo {}
// if you remove the Foo type annotation, (bar, baz) is (Bar, Baz) will return true,
// as the compiler can statically check the type.
let bar : Foo = Bar()
let baz : Foo = Baz()
print((bar, baz) is (Bar, Baz)) // false
class Bar {
let id : Int
init(id: Int) { self.id = id }
}
class Foo {
var bar : Bar
init(bar: Bar) { self.bar = bar}
func printBar() {
import Foundation
import QuartzCore
func benchmark(repeatCount: Int = 1, name: String = "closure", closure: () -> Void) {
let d = CACurrentMediaTime()
for _ in 0..<repeatCount {
closure()
}
let d1 = CACurrentMediaTime()-d
print("Benchmark of \(name) took \(d1) seconds")
import UIKit
struct ConstraintInfo {
var attribute: NSLayoutAttribute = .left
var secondAttribute: NSLayoutAttribute = .notAnAttribute
var constant: CGFloat = 0
var identifier: String?
var relation: NSLayoutRelation = .equal
}