pixeldock / RxAppState

RxSwift extensions for UIApplicationDelegate methods to observe changes in your app's state
MIT License
391 stars 45 forks source link

Can't access AppDelegate #6

Closed sutefan1 closed 7 years ago

sutefan1 commented 7 years ago

I need to access my AppDelegate, but when I do this let appDelegate = UIApplication.shared.delegate as! AppDelegate, an error occurs that I can't cast RxAppState.RxApplicationDelegateProxy to AppDelegate.

pixeldock commented 7 years ago

Normally you shouldn't have to do the cast. RxApplicationDelegateProxy confirms to UIApplicationDelegate and gives you access to all the methods and properties defined in the UIApplicationDelegate protocol.

sutefan1 commented 7 years ago

What about properties which are defined in AppDelegate? How can I access them without cast?

pixeldock commented 7 years ago

Ah, sorry I misunderstood. You can access the original app delegate via forwardToDelegate().

So if your AppDelegate has a customProperty property, you can access it like this:


if
   let delegateProxy = UIApplication.shared.delegate as? DelegateProxy,
   let appDelegate = delegateProxy.forwardToDelegate() as? AppDelegate {
      print(appDelegate.customProperty)
   }
shams-ahmed commented 7 years ago

@pixeldock is there a way to avoid doing any casting? just have it return Appdelegate while still having the ability to observe for changes

pixeldock commented 7 years ago

@shams-ahmed The DelegateProxy class is part of RxCocoa. I don‘t see any way around the casting.