Last active
August 29, 2015 14:22
-
-
Save xNekOIx/284e971bfaa71e65cddb to your computer and use it in GitHub Desktop.
Declare class inside function.
This file contains hidden or 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
protocol SomeProtocol: class { | |
func pew() -> String | |
} | |
class SomeClass { | |
func someFunc() -> SomeProtocol { | |
let someClosure = { () -> SomeProtocol in | |
class PewPew: SomeProtocol { | |
func pew() -> String { | |
return "Pew-pew" | |
} | |
} | |
return PewPew() | |
} | |
return someClosure() | |
} | |
} | |
let obj = SomeClass() | |
let objb = obj.someFunc() | |
let resultingString = objb.pew() |
This file contains hidden or 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
protocol PewShmewProtocol { | |
func pew() -> String | |
func shmew() -> String | |
} | |
class SomeClass { | |
var pewShmewHandler: PewShmewProtocol? | |
func pew() -> String? { | |
return pewShmewHandler?.pew() | |
} | |
func shmew() -> String? { | |
return pewShmewHandler?.shmew() | |
} | |
} | |
let obj = SomeClass() | |
obj.pewShmewHandler = { | |
struct PewShmew: PewShmewProtocol { | |
func pew() -> String { | |
return "Pew-pew" | |
} | |
func shmew() -> String { | |
return "Pew-shmew" | |
} | |
} | |
return PewShmew() | |
}() | |
obj.pew() | |
obj.shmew() | |
obj.pewShmewHandler = { | |
class PewShmew: PewShmewProtocol { | |
var c = 0 | |
func pew() -> String { | |
c += 1 | |
return "P-p \(c)" | |
} | |
func shmew() -> String { | |
c += 1 | |
return "P-sh \(c)" | |
} | |
} | |
return PewShmew() | |
}() | |
obj.pew() | |
obj.shmew() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment