在 Swift 中,这个特定的语法是什么意思? [英] in Swift, what does this specific syntax mean?

查看:31
本文介绍了在 Swift 中,这个特定的语法是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试按照一些 iOS Swift 教程来连接到 Parse.com

I've been attempting to follow some iOS Swift tutorials for connecting to Parse.com

codementor.io 教程:

  loginViewController.fields = .UsernameAndPassword | .LogInButton | .PasswordForgotten | .SignUpButton | .Facebook | .Twitter

makeschool 教程

  loginViewController.fields = [.UsernameAndPassword, .LogInButton, .SignUpButton, .PasswordForgotten, .Facebook]

我假设前者是 Switch 1.x,后者是 Swift 2.从上下文来看,它们似乎在做同样的事情,但我还没有找到语法变化的语言参考.很难搜索点,管道和逗号......有人可以解释每个片段中的语法吗?(我正在努力阅读语言规范,但让应用真正运行会很有趣!)

I assume the former is Switch 1.x, and thelatter is Swift 2. From context they appear to be doing the same thing, but I haven't yet found the language references for the change in syntax. Awfully hard to search for dots, pipes and commas... can someone explain the syntax in each snippet? (I'm working on reading through the language specification, but would be fun to actually get an app to work!)

推荐答案

旧的 Swift 1 语法基于您在 C 和 Objective-C 中处理选项集的方式:您以整数类型存储选项集并使用按位运算符(|&~)来操作它们.所以 .UsernameAndPassword |.LogInButton 表示包含 .UsernameAndPassword.LogInButton 选项的选项集.在旧语法中,您使用 nil 表示一个空选项集(其中不包含任何选项),根据非空集的语法,这并不明显.

The old Swift 1 syntax is based on the way you deal with option sets in C and Objective-C: you store an option set in an integer type and use bitwise operators (| and & and ~) to manipulate them. So .UsernameAndPassword | .LogInButton means an option set in which both the .UsernameAndPassword and the .LogInButton options are included. In the old syntax, you use nil to represent an empty option set (in which no options are included), which is not obvious based on the syntax for a non-empty set.

Chris Lattner 在WWDC 2015 Session 106: What's New in Swift 中描述了更改后的语法一>.首先他描述了旧语法的问题:

Chris Lattner described the changed syntax in WWDC 2015 Session 106: What's New in Swift. First he describes the problems with the old syntax:

问题是,当你接触到你最终使用的其他语法时,它就不那么好用了.你用 nil 创建了一个空选项集——这没有意义,因为选项集和选项是完全不同的概念,它们被混为一谈.你用按位运算来提取它们,这很痛苦,而且非常容易出错,而且很容易出错.

The problem is, when you get to the other syntaxes you end up using, it is a bit less nice. You create an empty-option set with nil -- it doesn't make sense because option sets and optionals are completely different concepts and they're conflated together. You extract them with bitwise operations, which is a pain and super error-prone, and you can get it wrong easily.

然后他描述了新方法:

但是 Swift 2 解决了这个问题.它使选项集类似设置.这意味着选项集和集现在由方括号组成.这意味着您将获得带有一组空方括号的空集,并且您将获得完整的标准集 API 来处理选项集.

But Swift 2 solves this. It makes option sets set-like. That means option sets and sets are now formed with square brackets. That means you get empty sets with an empty set of square brackets, and you get the full set of standard set API to work with option sets.

新语法之所以有效是因为OptionSetType 符合ArrayLiteralConvertible 协议(间接地,通过符合SetAlgebraType).该协议允许使用数组字面量初始化符合要求的对象,方法是使用带有元素列表的 init.

The reason the new syntax works is because OptionSetType conforms to the ArrayLiteralConvertible protocol (indirectly, by conforming to SetAlgebraType). This protocol allows a conforming object to be initialized using an array literal, by having an init that takes a list of elements.

在新的 Swift 2 语法中,[ .UsernameAndPassword, .LogInButton ] 表示包含 .UsernameAndPassword.LogInButton 的选项集> 选项.请注意,它看起来就像您可以初始化一个普通的旧 Set 的语法: let intSet: Set= [ 17, 45 ].新语法使您很明显指定了一个空选项集为 [].

In the new Swift 2 syntax, [ .UsernameAndPassword, .LogInButton ] represents an option set containing both the .UsernameAndPassword and the .LogInButton options. Note that it looks just like the syntax by which you can initialize a plain old Set: let intSet: Set<Int> = [ 17, 45 ]. The new syntax makes it obvious that you specify an empty option set as [].

这篇关于在 Swift 中,这个特定的语法是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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