Skip to content

Instantly share code, notes, and snippets.

@cliss
Created March 16, 2016 23:17
Show Gist options
  • Select an option

  • Save cliss/5716c3efb807ef078da1 to your computer and use it in GitHub Desktop.

Select an option

Save cliss/5716c3efb807ef078da1 to your computer and use it in GitHub Desktop.
Doing bad things with delegation
import UIKit
import CoreLocation
import XCPlayground
XCPlayground.XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
class LocationDelegate: NSObject, CLLocationManagerDelegate {
override var description: String {
get {
return "LocationDelegate"
}
}
}
extension CLLocationManager {
var myDelegate: LocationDelegate {
get {
guard self.delegate == nil else {
fatalError("You're shit outta luck.")
}
self.delegate = LocationDelegate()
// 👇 Calling this fails because self.delegate is nil.
return self.delegate as! LocationDelegate
}
}
}
var locationManager = CLLocationManager()
print(locationManager.myDelegate.description)
@cbguder
Copy link
Copy Markdown

cbguder commented Mar 17, 2016

Maybe I'm missing something but why do any of this? What's wrong with:

let locationDelegate = LocationDelegate()
let locationManager = CLLocationManager()
locationManager.delegate = locationDelegate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment