如何在
Swift中将协议作为参数传递?
在Objective-C中,我可以这样做:
id <CurrentUserContex> userContex = [ServiceLocator locate:@protocol(CurrentUserContex)];
服务定位器:
+ (id)locate:(id)objectType
编辑
在Qbyte回答尝试使用后:
ServiceLocator.locate(CurrentUserContex.self)
但我得到’CurrentUserContex.Protocol’没有确认协议’AnyObject’
所以我尝试过:
ServiceLocator.locate(CurrentUserContex.self as! AnyObject)
但后来我得到:
Could not cast value of type 'ApplicationName.CurrentUserContex.Protocol' (0x7fec15e52348) to 'Swift.AnyObject' (0x7fec15d3d4f8).