在swift中,如何调用具有多个协议的目标c消息?这是在我起床后我所有的桥头等。
Objc消息:

 NSString *TheDeviceId = [[[[MyLocationService instance] getTheDeviceType] getUserID] getUserValue];

Swift信息:
var TheDeviceId = MyLocationService.instance (getTheDeviceType: AnyObject ,getUserID: AnyObject , getUserValue: AnyObject)

最佳答案

这不是一条包含多个参数的消息。它是一系列管链消息——每个附加的方法调用都是针对前一个调用的返回值进行的。您的ObjC相当于(通过猜测一些中间类名):

MyLocationService *service = [MyLocationService instance];
MyDeviceType *theType = [service getTheDeviceType];
MyUserID *userID = [theType getUserID];
NSString *theDeviceID = [userID getUserValue];

一旦你看到这一点,在Swift中做同样的事情就很简单了:
let service = MyLocationService.instance()
let theType = service.getTheDeviceType()
let userID = theType.getUserID()
let theDeviceID = userID.getUserValue()

为了简洁起见,再把它串起来:
MyLocationService.instance().getTheDeviceType().getUserID().getUserValue()

关于objective-c - 如何在Swift中调用也具有多个参数的Objective C方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24960671/

10-09 06:45