I have a comprehension question. I want to use the Dropbox Objective-C framework in an iOS Swift app.
I already imported the framework successfully and set the import clause in the bridging header.
I was also able to run the authorization process so I think the framework works.
Then I try to use a component of the framework which is declared as protocol:
我有一个理解问题。我想在iOS Swift应用中使用Dropbox Objective-C框架。我已经成功导入框架,并在桥接头中设置了import子句。我也能够运行授权过程,所以我认为框架可以工作。然后我尝试使用框架的一个组件,它被声明为协议:
class ViewController: UIViewController, DBRestClientDelegate {
}
I sat the delegate property, called the loadMetadata method and implemented the corresponding event function:
我将委托属性设为loadMetadata方法,并实现相应的事件函数:
let dbRestClient = DBRestClient(DBSession.shared())
dbRestClient.delegate = self
dbRestClient.loadMetadata("/")
...
func restClient(client: DBRestClient!, loadedMetadata metadata: DBMetadata!) {
}
What I'm wondering is that it seems not necessary to implement all methods of that protocol. Is this correct? In Swift implementing only a part of a protocol is enough?
I ask because the compiler displays no errors but the delegation method is never called.
我想知道的是,似乎没有必要实现该协议的所有方法。这是正确的吗?在Swift中,只实现协议的一部分就足够了吗?我这样问是因为编译器不会显示错误,但是从来不会调用委托方法。
1 个解决方案
#1
1
Generally, in Swift you have to implement ALL methods of a protocol. (See this question about optional protocol methods: How to define optional methods in Swift protocol?)
通常,在Swift中,您必须实现协议的所有方法。(参见关于可选协议方法的问题:如何在Swift协议中定义可选方法?)
But as you said, the framework is written in Objective-C. Objective-C supports optional methods in protocols.
但是正如你所说的,框架是用Objective-C写的。Objective-C支持协议中的可选方法。
@protocol ProtocolName
@required
// list of required methods
@optional
// list of optional methods
@end
源
That's why you don't necessarily need to implement all methods declared in a protocol. Usually, only the most important methods are marked with @required
, because when calling an optional method, you should always check, if the delegate implemented it.
这就是为什么您不必实现协议中声明的所有方法。通常,只有最重要的方法被标记为@required,因为在调用可选方法时,应该始终检查委托是否实现了它。
#1
1
Generally, in Swift you have to implement ALL methods of a protocol. (See this question about optional protocol methods: How to define optional methods in Swift protocol?)
通常,在Swift中,您必须实现协议的所有方法。(参见关于可选协议方法的问题:如何在Swift协议中定义可选方法?)
But as you said, the framework is written in Objective-C. Objective-C supports optional methods in protocols.
但是正如你所说的,框架是用Objective-C写的。Objective-C支持协议中的可选方法。
@protocol ProtocolName
@required
// list of required methods
@optional
// list of optional methods
@end
源
That's why you don't necessarily need to implement all methods declared in a protocol. Usually, only the most important methods are marked with @required
, because when calling an optional method, you should always check, if the delegate implemented it.
这就是为什么您不必实现协议中声明的所有方法。通常,只有最重要的方法被标记为@required,因为在调用可选方法时,应该始终检查委托是否实现了它。