【问题标题】:Swift not conforming to Objective-C protocolSwift 不符合 Objective-C 协议
【发布时间】:2015-11-27 02:25:50
【问题描述】:

所以我正在尝试遵循 wsdl2code 中的教程,他们在应用程序委托中实现 obj-c 协议(整个项目都在 Objective-c 中)。

我正在尝试快速重新创建它,但我一直被告知我不符合协议。我已经确保方法的 swift 版本中使用的类型正确地从 Objective-c 切换到 swift。

这是objective-c的头文件

#ifndef _Wsdl2CodeProxyDelegate
#define _Wsdl2CodeProxyDelegate
@protocol Wsdl2CodeProxyDelegate
//if service recieve an error this method will be called
-(void)proxyRecievedError:(NSException*)ex InMethod:(NSString*)method;
//proxy finished, (id)data is the object of the relevant method service
-(void)proxydidFinishLoadingData:(id)data InMethod:(NSString*)method;
@end
#endif

这是我的快速代码

class AppDelegate: UIResponder, UIApplicationDelegate, Wsdl2CodeProxyDelegate {

    // MARK: Proxy protocol methods

    func proxydidFinishLoadingData(data: AnyObject!, inMethod method: String!) {
        print("Service \(method) done!")
    }

    func proxyRecievedError(ex: NSException!, inMethod method: String!) {
        print("Exception in service \(method)")
    }

【问题讨论】:

  • 除了 Ethan 在下面指出的之外,ObjC 标头中的方法名称命名不佳 (...InMethod),但在 swift 中,您正在使用“inMethod”
  • 这就是 xcode 的要求。它是骆驼式的,如果我改变它,它会告诉我它不像方法

标签: objective-c xcode swift


【解决方案1】:

你应该像这样实现协议,'Wsdl2CodeProxyDelegate'忘记实现'NSObject'协议

@protocol Wsdl2CodeProxyDelegate <NSObject>
//if service recieve an error this method will be called
- (void)proxyRecievedError:(NSException*)ex inMethod:(NSString*)method;
//proxy finished, (id)data is the object of the relevant method service
- (void)proxydidFinishLoadingData:(id)data inMethod:(NSString*)method;
@end

func proxydidFinishLoadingData(data: AnyObject!, inMethod method: String!) {

}

func proxyRecievedError(ex: NSException!, inMethod method: String!) {

}

【讨论】:

  • 没用。它仍然说它没有被遵守。
  • 你们俩是不是忘了把String改成NSString?还有inMethod vs InMethod
  • 我不得不说你写错了'Wsdl2CodeProxyDelegate',你一开始就走错了方向。更新我的回复,在我的项目中运行良好。
猜你喜欢
  • 1970-01-01
  • 2015-02-05
  • 1970-01-01
  • 2018-05-16
  • 1970-01-01
  • 2020-11-19
  • 2015-10-28
  • 2015-04-27
相关资源
最近更新 更多