这是我试过迄今为止,但它会产生语法错误。
Use of undeclared IDentifIEr T.
我究竟做错了什么?
protocol APIMapperProtocol { func MapFromSource(T) -> U}class UserMapper: NSObject,APIMapperProtocol { func MapFromSource(data: NSDictionary) -> usermodel { var user = usermodel() as usermodel var accountsData:NSArray = data["Accounts"] as NSArray return user } }对于协议有点不同。看看“关联类型” in Apple’s documentation。
这是你在你的例子中使用它
protocol APIMapperProtocol { associatedtype T associatedtype U func MapFromSource(T) -> U}class UserMapper: NSObject,APIMapperProtocol { typealias T = NSDictionary typealias U = usermodel func MapFromSource(data:NSDictionary) -> usermodel { var user = usermodel() var accountsData:NSArray = data["Accounts"] as NSArray // For Swift 1.2,you need this line instead // var accountsData:NSArray = data["Accounts"] as! NSArray return user }}总结
以上是内存溢出为你收集整理的如何在Swift中创建通用协议?全部内容,希望文章能够帮你解决如何在Swift中创建通用协议?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)