0

In Xcode I am having issues between watchOS and iOS. The following code is the same for both targets:

watchOS

class ExtensionDelegate: NSObject, WKExtensionDelegate {
    var variable1: Float?
}

iOS

class AppDelegate: UIResponder, UIApplicationDelegate {
    var variable1: Float?
}

the class

class SomeClass: ObservableObject {
    
    #if os(iOS)
    let delegate = UIApplication.shared.delegate as! AppDelegate
    #elseif os(watchOS)
    let delegate = WKExtension.shared().delegate as! WKExtension
    #endif

func somefunc(){
delegate.variable1 = 0.0 // Fine when target is iOS, fails when watchOS with WKExtension has no member variable1
   }
}

I am not sure why that delegate.variable1 is invalid for watchOS but not iOS

Michael Rowe
  • 870
  • 2
  • 11
  • 27
  • `WKExtension` is the extension base class. You want `as! ExtensionDelegate` That forced downcast should give you a warning that it will fail and should crash at runtime – Paulw11 Sep 25 '20 at 03:27
  • Thanks.. I am now getting a nil in the Initializer of SomeClass at the line of let delegate = WKExtension.shared().delegate as! ExtensionDelegate – Michael Rowe Sep 25 '20 at 14:04
  • Per @paulw11 this was a simple update... so closing the question. – Michael Rowe Sep 26 '20 at 14:49

0 Answers0