Skip to content

Instantly share code, notes, and snippets.

@speaktoalvin
Forked from austinzheng/main.swift
Last active August 29, 2015 14:12
Show Gist options
  • Save speaktoalvin/5103b9b8c58f04e96164 to your computer and use it in GitHub Desktop.
Save speaktoalvin/5103b9b8c58f04e96164 to your computer and use it in GitHub Desktop.
import Foundation
protocol SomeDelegateProtocol : class {
func firstFunc() -> String
func secondFunc() -> Bool
func thirdFunc() -> Self
}
class MyClass {
weak var delegate : SomeDelegateProtocol?
}
class Parent {
var child : MyClass
var strongDelegate : SomeDelegateProtocol? = nil
func setup() {
strongDelegate = {
// [self] // Original version had this not commented; it's commented to let people know it was a typo
final class SomethingClass : SomeDelegateProtocol {
func firstFunc() -> String { return "foo" }
func secondFunc() -> Bool { return true }
func thirdFunc() -> SomethingClass { return SomethingClass() }
}
return SomethingClass()
}()
child.delegate = strongDelegate
}
init() {
self.child = MyClass()
self.setup()
}
}
var bleh = Parent()
var result = bleh.child.delegate?.firstFunc()
// selfResult is inferred to be type 'Optional<SomethingDelegateProtocol>'.
let selfResult = bleh.child.delegate?.thirdFunc()
println("Got \(result!)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment