Why is my simple swift delegate and protocol setup not working? - ios

I am trying to grasp the concept of delegates and protocols in swift. So I have implemented my own PlayableMedia protocol with two concrete classes BlueRayMedia and DVDMedia like so:
#protocol PlayableMedia {
func play()
func stop()
}
class BlueRayMedia:PlayableMedia {
func play() {
println("BlueRayMedia is playing")
}
func stop() {
println("BlueRayMedia has stopped playing")
}
}
class DVDMedia:PlayableMedia {
func play() {
println("DVD is playing")
}
func stop() {
println("DVD has stopped playing")
}
}
So now I have a DVDPlayer class that uses this setup:
class DVDPlayer {
var media:PlayableMedia // delegate property
init(media:PlayableMedia){
self.media = media
}
func didStartPlaying() {
media.play()
}
func didStopPlaying() {
media.stop()
}
}
But when I try to use it like this:
var dvdPlayer:DVDPlayer = DVDPlayer(media: BlueRayMedia())
dvdPlayer.didStartPlaying()
I get (no results) in my playground console. What am I doing wrong?

Ok so the simple mistake I made was use #protocol instead of just protocol
So this works:
protocol PlayableMedia {
func play()
func stop()
}

Inside the Playground println() doesn't work.
Add some other expression like let x = 5

Related

Share implementations with protocols

I have two presenters: FirstPresenter and SecondPresenter. FirstPresenter conform to protocol PresenterProtocol. SecondPresenter needs to use all functions from FirstPresenter but with two additional.
protocol PresenterProtocol: class {
func one()
func two()
func third()
}
class FirstPresenter: PresenterProtocol {
func one() {
// do something
}
func two() {
// do something
}
func third() {
// do something
}
}
And then I have SecondPresenter and I need to use exactly the same implementation from FirstPresenter (but I want to avoid inheritance, I want do it with protocols)
class SecondPresenter: PresenterProtocols {
var firstPresenter: PresenterProtocol = FirstPresenter()
func one() {
firstPresenter.one()
// do something
}
func two() {
firstPresenter.two()
// do something
}
func third() {
firstPresenter.third()
// do something
}
func additionalFunction() {
// do something more
}
}
I am not sure if calling firstpresenter function is a good way to solve this problem, because it's just rewriting. I wonder also to use default implementation. What's the best way to share functionalities?
Your code doesn't actually rely on the methods you define, so they don't need to be requirements. They're extensions. As written, your code would be:
// This is where *requirements* go. Not shared code.
protocol PresenterProtocol: class {}
// This is your shared code
extension PresenterProtocol {
func one() {
// do something
}
func two() {
// do something
}
func third() {
// do something
}
}
// And FirstPresenter needs nothing else
class FirstPresenter: PresenterProtocol {}
// SecondPresenter gets those, and also has other things
class SecondPresenter: PresenterProtocols {
func additionalFunction() {
// do something more
}
}
Now, I'm betting that one() actually has some requirements. It needs its implementers to provide something. Those are what go in your PresenterProtocol. For example:
extension PresenterProtocol {
func one() {
doFirstThing() // Something the implementer must do
doSecondThing() // Something the implementer must do
}
}
In that case, you'd add those as requirements:
protocol PresenterProtocol {
func doFirstThing()
func doSecondThing()
}
And if there were a default way to do it that some implementers might override, then you'd provide a default implementation. Or you can just have all implementers provide it directly. Or you might not have any requirements at all.
Having a parent class is the better implementation as far as I see. But, if you don't want to, swift protocols have a nifty trick: the protocols can be extended.
Let me demonstrate using your code,
protocol PresenterProtocol: class {
func one()
func two()
func third()
}
// Adding the extenstion/default implementation
extension PresenterProtocol {
func one() {
print("one was pressed")
}
func two() {
print("two was pressed")
}
func third() {
print("third was pressed")
}
}
This way, any class conforming to PresenterProtocol will use the so-called "default implementation" (which is another way to say protocol extensions) unless you override the method in the class.
So your usage will look something like the following where you don't need to implement the 3 methods all over again.
class SecondPresenter: PresenterProtocol {
// Calling default implementations
func someFunction() {
one()
two()
third()
}
}
.
.
.
class FirstPresenter: PresenterProtocol {
func someFunction() {
one()
two()
third()
}
}
If you want more help learning, I would highly suggest reading the HackingWithSwift Tutorial

Empty vars when calling method from singleton class

So I have a singleton class in which I have implemented the delegate of my module. However when that delegate method gets executed in this singleton I call a method in another class of the App and it crashes because all the variables previously set in that class are empty.
class Player: Jukebox, JukeboxDelegate {
static let sharedInstance = Player()
func setDelegate(){
jukebox = Jukebox(delegate: self)
}
func play(Link: String) {
jukebox.setIT([JukeboxItem(URL: NSURL(string: Link)!)])
jukebox.setImage(currentImage)
jukebox?.play()
}
func audioPlayerDidFinishPlaying(player: Jukebox, successfully flag: Bool) {
if pltype == PlayerType.Playlist {
MyMusicVC.continuePlaying() { () -> () in
}
}
}
func jukeboxStateDidChange(state: Jukebox) {
}
func jukeboxPlaybackProgressDidChange(jukebox: Jukebox) {
}
func jukeboxDidLoadItem(jukebox: Jukebox, item: JukeboxItem) {
}
func jukeboxDidUpdateMetadata(jukebox: Jukebox, forItem: JukeboxItem) {
}
}
Any ideas on why could this be happening?
Its hard to understand what is going on. But you are subclassing a Jukebox class. Then making this subclass a delegate of the Jukebox itself. Then I also notice you're setting a Jukebox property that appears to be optional but not.
For example. Why in the play() function is jukebox. used twice but the last line is jukebox?. Is jukebox optional? Or implicitly unwrapped? And if thats the case, why are you using it unwrapped twice and then optionally the third time.
Have you tried not making this singleton a subclass of jukebox. Instead just make it conform to the JukeBoxDelegate protocol and see if that accomplishes the same goal?

Swift Public protocols with Internal functions and properties

I am wondering what the best practice is when I want some functions to be public and some to me internal when working with protocols.I am writing an AudioManager in Swift 3 wrapping AVPlayer as a framework. I want some methods to be public, so that e.g. a ViewController making use of the AudioManager can access some methods, but some methods would not be exposed outside the framework -> i.e. having the access modifier internal instead of public. I am writing the framework with protocol driven design, almost every part should have a protocol.So protocols are talking to protocols within the framework. E.g. the main class - AudioManager - has an AudioPlayer, and should be able to call some internal functions on it, e.g. pause(reason:) but that method should be internal and not exposed outside the framework.Here is an example.
internal enum PauseReason {
case byUser
case routeChange
}
// Compilation error: `Public protocol cannot refine an internal protocol`
public protocol AudioPlayerProtocol: InternalAudioPlayerProtocol {
func pause() // I want
}
internal protocol InternalAudioPlayerProtocol {
func pause(reason: PauseReason) // Should only be accessible within the framework
}
public class AudioPlayer: AudioPlayerProtocol {
public func pause() {
pause(reason: .byUser)
}
// This would probably not compile because it is inside a public class...
internal func pause(reason: PauseReason) { //I want this to be internal
// save reason and to stuff with it later on
}
}
public protocol AudioManagerProtocol {
var audioPlayer: AudioPlayerProtocol { get }
}
public class AudioManager: AudioManagerProtocol {
public let audioPlayer: AudioPlayerProtocol
init() {
audioPlayer = AudioPlayer()
NotificationCenter.default.addObserver(self, selector: #selector(handleRouteChange(_:)), name: NSNotification.Name.AVAudioSessionRouteChange, object: nil)
}
func handleRouteChange(_ notification: Notification) {
guard
let userInfo = notification.userInfo,
let reasonRaw = userInfo[AVAudioSessionRouteChangeReasonKey] as? NSNumber,
let reason = AVAudioSessionRouteChangeReason(rawValue: reasonRaw.uintValue)
else { print("what could not get route change") }
switch reason {
case .oldDeviceUnavailable:
pauseBecauseOfRouteChange()
default:
break
}
}
}
private extension AudioManager {
func pauseBecauseOfRouteChange() {
audioPlayer.pause(reason: .routeChange)
}
}
// Outside of Audio framework
class PlayerViewController: UIViewController {
fileprivate let audioManager: AudioManagerProtocol
#IBAction didPressPauseButton(_ sender: UIButton) {
// I want the `user of the Audio framwwork` (in this case a ViewController)
// to only be able to `see` `pause()` and not `pause(reason:)`
audioManager.audioPlayer.pause()
}
}
I know I can get it to work by changing the method pauseBecauseOfRouteChange to look like this:
func pauseBecauseOfRouteChange() {
guard let internalPlayer = audioPlayer as? InternalAudioPlayerProtocol else { return }
internalPlayer.pause(reason: .routeChange)
}
But I am wondering if there is a more elegant solution? Something like marking that the AudioPlayerProtocol refines the InternalAudioPlayerProtocol...
Or how do you fellow programmers do it?The framework is more beautiful if it does not expose methods and variables that are intended for internal use!
Thanks!
No, there is no more elegant solution to this, at least when considering protocols, and here is why:
Imagine a scenario that someone using your framework wants to write an extension for the AudioPlayerProtocol, how then pause(reason:) method can be implemented if it's internal?
You can achieve it by just subclassing and this code actually will compile:
public class AudioPlayer: AudioPlayerProtocol {
public func pause() {
pause(reason: .byUser)
}
internal func pause(reason: PauseReason) {
}
}
With protocols this is not the case, because you simply cannot guarantee implementation of internal function if someone with public access level wants to use your mixed public/internal protocol.
How about if you split your protocol into internal and public and then let the public implementation class delegate to an internal implementation. Like so
internal protocol InternalAudioPlayerProtocol {
func pause(reason: PauseReason)
}
public protocol AudioPlayerProtocol {
func pause()
}
internal class InternalAudioPlayer: InternalAudioPlayerProtocol {
internal func pause(reason: PauseReason) {
}
}
public class AudioPlayer: AudioPlayerProtocol {
internal var base: InternalAudioPlayerProtocol
internal init(base: InternalAudioPlayerProtocol) {
self.base = base
}
public func pause() {
base.pause(reason: .byUser)
}
}
public protocol AudioManagerProtocol {
var audioPlayer: AudioPlayerProtocol { get }
}
public class AudioManager: AudioManagerProtocol {
internal let base = InternalAudioPlayer()
public let audioPlayer: AudioPlayerProtocol
public init() {
audioPlayer = AudioPlayer(base: base)
}
internal func handleSomeNotification() {
pauseBecauseOfRouteChange() //amongst other things
}
internal func pauseBecauseOfRouteChange() {
base.pause(reason: .routeChange)
}
}
It's an old topic but what one can do is actually the opposite.
Instead of publicProtocol extending internalProtocol have internalProtocol extending publicProtocol.
public protocol AudioPlayerProtocol {
func pause() // I want
}
internal protocol InternalAudioPlayerProtocol: AudioPlayerProtocol {
func pause(reason: PauseReason) // Should only be accessible within the framework
}
public class AudioPlayer: InternalAudioPlayerProtocol {
public func pause() {
pause(reason: .byUser)
}
internal func pause(reason: PauseReason) {
//Do stuff
}
}
Then in the manager
public class AudioManager: AudioManagerProtocol {
public let audioPlayer: AudioPlayerProtocol
private let intAudioPlayer: InternalAudioPlayerProtocol
init() {
intAudioPlayer = AudioPlayer()
audioPlayer = intAudioPlayer
...
}
...
private func pauseBecauseOfRouteChange() {
intAudioPlayer.pause(reason: .routeChange)
}
}

Lazy function in Swift

Could anyone tell me why in this 'odd' code (I'm having fun with Swift ;D) in lazy functions runEngine and stopEngine, print method is never executed?
(please run this code in playground).
Thanks!
protocol EngineDelegate {
func engineDidStart()
func engineDidStop()
}
class Engine {
var delegate: EngineDelegate?
lazy var runEngine : () -> () = {
print("Engine has been started")
self.delegate?.engineDidStart()
}
lazy var stopEngine : () -> () = {
print("Engine has been stoped")
self.delegate?.engineDidStop()
}
}
class Car: EngineDelegate {
let engine = Engine()
init() {
engine.delegate = self
}
func engineDidStop() {
print("MyOwnStop")
}
func engineDidStart() {
print("MyOwnStart")
}
}
let car = Car()
car.engine.runEngine()
The code runs as expected for me.
At first I thought that the lazy modifier was unnecessary but it is. When Engine is instantiated, its delegate is nil and that value is what is captured by the closure. Using lazy deferred that capture until its use which by that time engine.delegate had been set. While we might be able to use #autoclosure somehow, the best solution is to just make runEngine and stopEngine functions.
func runEngine() {
print("Engine has been started")
delegate?.engineDidStart()
}
func stopEngine() {
print("Engine has been stoped")
delegate?.engineDidStop()
}

Swift 2.0 Protocol as a Type Extension

While playing around in playground with protocol extension I came to strange error. More precisely I declared a protocol and used it as type in the following manner :
protocol InvokeProtocol{
func invokeA()
func invokeB()
}
class Controller{
var invoker : InvokeProtocol?
func methodA(){
invoker?.invokeA()
}
func methodB(){
invoker?.invokeB()
}
}
What I did next was creating a subclass of Controller and tried to extend the InvokeProtocol for that particular type
extension InvokeProtocol where Self: SpecificController{
func invokeC(){
}
}
class SpecificController : Controller {
override func methodA() {
super.methodA()
}
override func methodB() {
super.methodB()
}
func methodC(){
invoker?.invokeC()
}
}
But this give me the following compile time error while I try to call invokeC on SpecificController
#opened("70A8B09C-65E3-11E5-9A8E-6C40088AF546") InvokeProtocol' is not a subtype of 'SpecificController'
I just jumped to Swift 2.0 and this is my first time I see errors starting with #opened. What does this mean? Is this a known bug waiting to be fixed? If so do you guys have any workaround about this?
To reply to your comment. It is achievable. You just haven't implemented the protocol in the first place. The following will work:
protocol InvokeProtocol {
func invokeA()
func invokeB()
}
class Controller : InvokeProtocol {
func invokeA(){
}
func invokeB() {
}
}
extension InvokeProtocol where Self: SpecificController{
func invokeC() {
}
}
class SpecificController : Controller {
override func invokeA() {
super.invokeA()
}
override func invokeB() {
super.invokeB()
}
func invokeC() {
}
}
I guess the where Self: SpecificController, the Self means var invoke: InvokeProtocol?
if invoke conform the protocol and invoke is SpecificController, that invoke can call the method named invokeC()

Resources