Swift: Making the Associated Type Parameter Readable in Protocols

[英文教程]
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
查看查看64 回复回复1 收藏收藏 分享淘帖 转播转播 分享分享 微信
查看: 64|回复: 1
收起左侧

[英文教程] Swift: Making the Associated Type Parameter Readable in Protocols

[复制链接]
SwiftRobot 发表于 2016-10-26 06:38:17 | 显示全部楼层 |阅读模式
快来登录
获取最新的苹果动态资讯
收藏热门的iOS等技术干货
拷贝下载Swift Demo源代码

I’m still getting used to Associated Types in Swift. Even though they’ve been around for a while, I first startingto understand them from this talk by @alexisgallagher .

Swift: Making the Associated Type Parameter Readable in Protocols

Swift: Making the Associated Type Parameter Readable in Protocols - 敏捷大拇指 - Swift: Making the Associated Type Parameter Readable in Protocols


So I was happy yesterday when I used them to sort of solve a common problem in iOS development: View Controller Data Injection with Storyboards and Segues in Swift .

I updated my blog post from yesterday, but my initial Protocol looked something like this:

[Swift] 纯文本查看 复制代码
protocol Injectable {
 
    associatedtype T
    func inject(thing: T)
    func assertDependencies()
}


Notice the thing ! Since each View Controller would be injected with something specific – it could be text-based or number-based or array-based, anything! I didn’t know how to name the parameter. So thing seemed like the most fitting parameter name.

The implementation was something like this then:

[Swift] 纯文本查看 复制代码
func inject(thing: T) {
    textDependency = thing
}


I didn’t exactly like the whole thing here – it’s not that great with readability. So this morning, I though of a crazy solution for this, tried it out, and it actually worked!

[Swift] 纯文本查看 复制代码
protocol Injectable {
    associatedtype T
 
    // instead of thing, use _
    func inject(_: T)
}


Instead of using thing , I leave the parameter name blank in the protocol!

Apparently, this now let’s me name the parameter anything I want in the implementation!

[Swift] 纯文本查看 复制代码
class MyStringDependencyViewController: UIViewController, Injectable {
    
    typealias T = String
    privatevar textDependency: String!
    
    // here, the thing is text!
    func inject(text: String) {
        textDependency = text
    }
}
 
class MyIntDependencyViewController: UIViewController, Injectable {
    
    typealias T = Int
    privatevar numberDependency: Int!
    
    // here the thing is number!
    func inject(number: Int) {
        numberDependency = number
    }
}


Now, the implementation is super clear! The more I use associated types, the more I like them!




英文

都看到这里了,就把这篇资料推荐给您的好朋友吧,让他们也感受一下。

回帖是一种美德,也是对楼主发帖的尊重和支持。

*声明:敏捷大拇指是全球最大的Swift开发者社区、苹果粉丝家园、智能移动门户,所载内容仅限于传递更多最新信息,并不意味赞同其观点或证实其描述;内容仅供参考,并非绝对正确的建议。本站不对上述信息的真实性、合法性、完整性做出保证;转载请注明来源并加上本站链接,敏捷大拇指将保留所有法律权益。如有疑问或建议,邮件至marketing@swifthumb.com

*联系:微信公众平台:“swifthumb” / 腾讯微博:@swifthumb / 新浪微博:@swifthumb / 官方QQ一群:343549891(满) / 官方QQ二群:245285613 ,需要报上用户名才会被同意进群,请先注册敏捷大拇指

嗯,不错!期待更多好内容,支持一把:
支持敏捷大拇指,用支付宝支付10.24元 支持敏捷大拇指,用微信支付10.24元

评分

参与人数 1金钱 +10 收起 理由
Anewczs + 10 发帖就是光荣,分享精神可嘉!.

查看全部评分

相关帖子