shaka-project / shaka-player-embedded

Shaka Player in a C++ Framework
Apache License 2.0
239 stars 62 forks source link

Decouple client setting and initialisation #117

Closed jgongo closed 4 years ago

jgongo commented 4 years ago

The ShakaPlayer currently provides a single initializer: init(client: ShakaPlayerClient!) where ShakaPlayerClient is a protocol with all its methods marked as optional.

After inspecting the code:

Could you please decouple initialisation from client setting? This way:

TheModMaker commented 4 years ago

I'll be fixing #94 to add the ShakaPlayer instance to the callbacks; but note that for initialization errors, the value passed will probably be nil. This is because the object failed to initialize, so the object is unusable.

The client can be nil (though that may not appear in Swift), and I'll add an empty init method for convenience.

I don't think we can use Swift error handling since that wouldn't work well in Objective-C. Exceptions in Objective-C are supposed to be programming errors and fatal errors; most apps won't catch them. An alternative would be to use an NSError** pointer to set the error in init. Then you could do something like:

var err: ShakaPlayerError = nil;
guard let player = ShakaPlayer(client: self, error: &err) else {
  print("Error: \(err.message)")
}
jgongo commented 4 years ago

Hi @TheModMaker Having a parameter of type NSerror** as the last parameter of any Objective-C method (including an initialiser) translates into a Swift method with that last parameter removed and with a throws. That's why in #116 I requested ShakaPlayerError to inherit from NSError

Regarding throwing an exception in this case... you would only throw it in Swift. In Objective-C you would be passing back the error in the NSError** parameter.

jgongo commented 4 years ago

With the modification provided in the #119 PR, the init method from Swift is seen as init() throws, while you can still use the initWithError:(NSError **)error signature when used from Objective C.