Swift 中大写“Self"的区别和小写的“self" [英] Distinction in Swift between uppercase "Self" and lowercase "self"

查看:46
本文介绍了Swift 中大写“Self"的区别和小写的“self"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Swift 游乐场玩耍时,我注意到带有大写S"的 Self 与小写的 self 一起可用.它们之间有什么区别吗?如果是这样,这两个有什么用途,特别是对于Self?

While playing in a Swift playground I noticed that Self, with capital "S", is available along with the lowercase self. Is there any difference between them? If so, what are usages for these two, especially for Self?

推荐答案

Self 指的是协议中当前事物"的类型(任何符合协议的东西).有关其使用示例,请参阅返回 Self 的协议函数.

Self refers to the type of the current "thing" inside of a protocol (whatever is conforming to the protocol). For an example of its use, see Protocol func returning Self.

我为 Self 找到的官方文档位于 协议关联类型声明.令人惊讶的是,它没有记录在协议或嵌套类型的部分中:

The official docs I've found for Self is in Protocol Associated Type Declaration in The Swift Programming Language. It surprisingly is not documented in the sections on protocols or on nested types:

然而,现在有一个关于Self Type 在官方 Swift 编程语言的 章节中包含一个代码示例类型

这篇关于Swift 中大写“Self"的区别和小写的“self"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆