3
votes

I am using Fusuma cocoal pod framework. However, I found that they have an Objective-C protocol even thought the whole project was written in Swift.

@objc public protocol FusumaDelegate: class {

    func fusumaImageSelected(image: UIImage)
    optional func fusumaDismissedWithImage(image: UIImage)
    func fusumaVideoCompleted(withFileURL fileURL: NSURL)
    func fusumaCameraRollUnauthorized()

    optional func fusumaClosed()
}

I want to add another function in the protocol. However, I am getting this warning

Method cannot be a member of an @objc protocol because the type of the parameter cannot be represented in objective-C

I am thinking about removing the @objc in front of the protocol but then it says I have to remove optional func. I don't mind changing them to just normal func, however, I want to find out what should I be aware of if I remove @objc

The function that I am trying to add is another delegate function for the previous VC. I have 3 VCs in this case. The 2nd one which is this one grabs the data from 3rd VC after it was dismissed. Then I want to add the following delegate function to allow VC1 to start uploading the data

extension FusumaViewController: VC3Delegate {
    func readyToUploadPost(postUpload: PostUpload) {
    delegate_?.readyToUploadPost_Fusuma(postUpload)
}
1
Likely they need the dynamic capabilities of Objective-C. You cannot do that in Swift. - Amin Negm-Awad
What does the function you are trying to add look like? - Mike Taverne
@MikeTaverne, please see the updated question that addresses your question. I tried to deleate the optional func and remove the objC, but it gives me an error saying Method cannot be declaredpublic because its parameter uses an internal type. So I ended up just creating a completely seperate protocol to deal with it. Not sure if that is the right way to do it though. Seems like a hack - user172902
It seems to be complaining that PostUpload is not representable in Objective-C. Does PostUpload inherit from NSObject? - Mike Taverne
@AminNegem-Awad this has nothing to do with the dynamic behaviors of Objective-C but with the absence of optional protocol requirements in Swift. - HAS

1 Answers

2
votes

Has @HAS stated the most likely reason this protocol was given the @objc is because Swift doesn't allow you to use optional protocol requirement methods. The swift way of doing this would be:

public protocol FusumaDelegate: class {

    func fusumaImageSelected(image: UIImage)
    func fusumaVideoCompleted(withFileURL fileURL: NSURL)
    func fusumaCameraRollUnauthorized()
}

extension FusumaDelegate {
    func fusumaDismissedWithImage(image: UIImage) {/*Default Implementation*/}
    func fusumaClosed() {/*Default Implementation*/}
}

Ofcourse, then you have the problem of a default implementation, I suppose, but:

You can use protocol extensions to provide a default implementation to any method or computed property requirement of that protocol. If a conforming type provides its own implementation of a required method or property, that implementation will be used instead of the one provided by the extension. The Swift Programming Language

This was created in a playground to give you an idea, without any errors:

public protocol FusumaDelegate: class {
    func fusumaImageSelected(image: UIImage)
    func fusumaVideoCompleted(withFileURL fileURL: NSURL)
    func fusumaCameraRollUnauthorized()
}

extension FusumaDelegate {
    func fusumaDismissedWithImage(image: UIImage) {}
    func fusumaClosed() {}
}


class Test : UIViewController, FusumaDelegate {
    func fusumaImageSelected(image: UIImage) {
        //do stuff
    }

    func fusumaVideoCompleted(withFileURL fileURL: NSURL) {
        //do stuff
    }

    func fusumaCameraRollUnauthorized() {
        //do stuff
    }

    func fusumaDismissedWithImage(image: UIImage) {
        //do stuff
        self.fusumaClosed()
    }
}