Operator and Item

1. ..<

for-in loop and the half-open range operator (..<)

         // Check each pair of items to see if they are equivalent.
for i in ..<someContainer.count {
if someContainer[i] != anotherContainer[i] {
return false
}
}

2. Generic Where Clauses

 extension Promise where T: Collection { // From PromiseKit library
// .......
}

Ref: Generic Where Clauses

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Generics.html

3. trailing closure

 /**
If you need to pass a closure expression to a function as the function’s final argument
and the closure expression is long, it can be useful to write it as a trailing closure
instead. A trailing closure is written after the function call’s parentheses, even though
it is still an argument to the function.
*/ func someFunctionThatTakesClosure(a :Int, closure: () -> Void) {
closure()
} // Here's how you call this function without using a trailing closure: someFunctionThatTakesClosure(a: , closure: {
// closure's body goes here
print("closure as a parameter.")
}) // Here's how you call this function with a trailing closure instead: someFunctionThatTakesClosure(a: ) {
// trailing closure's body goes here
print("trailing closure.")
} extension Int {
func repetitions(task: (_ th: Int) -> Void) {
for i in ..< self { // "for _ in a..<b" ??
task(i)
}
}
} let iExtension: Int =
iExtension.repetitions { (th:Int) in // arguments for Closure
print("\(th)th loop.")
}
print("+++++")
iExtension.repetitions { th in     // arguments for Closure
print("\(th)th loop.")
}
 print("2. +++++++++")
iExtension.repetitions( task: { th in // arguments
print("without trailing closure \(th)")
})

Ref:

What is trailing closure syntax?

https://www.hackingwithswift.com/example-code/language/what-is-trailing-closure-syntax

4. Closure Expression Syntax

{ (parameters) -> return type in

  statements

}

4.1 Shorthand Argument Names {$0, $1, $2}

 reversedNames = names.sorted(by: { (s1: String, s2: String) -> Bool in
return s1 > s2
}) // Inferring Type From Context
reversedNames = names.sorted(by: { s1, s2 in return s1 > s2 } ) // Implicit Returns from Single-Expression Closures
// Single-expression closures can implicitly return the result of their single
// expression by omitting the return keyword from their declaration
reversedNames = names.sorted(by: { s1, s2 in s1 > s2 } ) // Swift automatically provides shorthand argument names to inline closures,
// which can be used to refer to the values of the closure’s
// arguments by the names $0, $1, $2
reversedNames = names.sorted(by: { $ > $ } )

5. guard statement

guard statement is used to transfer program control out of a scope if one or more conditions aren’t met.

guard statement has the following form:

guard condition else {

  statements

}

 func test_guard(_ i: Int) /* -> Void */ {

     guard i >  else {
print("In test_guard() else clause.")
return
} print("In test_guard() the lastest statement.")
} test_guard() // Output:
In test_guard() else clause.

Ref:

1. Guard Statement

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html

6. ? and ! [*]

6.1 "?"

如下所示, 类型后的"?"表示什么:

 func handleLocation(city: String?, state: String?,
latitude: CLLocationDegrees, longitude: CLLocationDegrees) {
//......
}

6.2 "!"

如下所示, "!"表示什么:

 let urlString = "http://api.openweathermap.org/data/2.5/weather?lat=" + "\(latitude)&lon=\(longitude)&appid=\(appID)"
let url = URL(string: urlString)!
let request = URLRequest(url: url)

7. backtick(`) in identifier

下面的代码中 "final func `catch` (" 为什么需要"`"符号:

     final func `catch`(on q: DispatchQueue, policy: CatchPolicy, else resolve: @escaping (Resolution<T>) -> Void, execute body: @escaping (Error) throws -> Void) {
pipe { resolution in
switch (resolution, policy) {
case (.fulfilled, _):
resolve(resolution)
case (.rejected(let error, _), .allErrorsExceptCancellation) where error.isCancelledError:
resolve(resolution)
case (let .rejected(error, token), _):
contain_zalgo(q, rejecter: resolve) {
token.consumed = true
try body(error)
}
}
}
}

"To use a reserved word as an identifier, put a backtick (`) before and after it. For example,

class is not a valid identifier, but `class` is valid. The backticks are not considered part of the

identifier; `x` and x have the same meaning."

Ref: Identifiers

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html

8. "default" keyword in Swift parameter

有这种情况,在Xcode中查看一些系统library的"头文件"(Swift中并没有Header File的概念)时,会遇到下面的情况:

 extension DispatchQueue {
// ......
public func async(group: DispatchGroup? = default, qos: DispatchQoS = default, flags: DispatchWorkItemFlags = default, execute work: @escaping @convention(block) () -> Swift.Void)
// ......
}

"flags: DispatchWorkItemFlags = default"中有default keyword。

"This is not a valid Swift code, it's generated on the fly." Ref[1]

"You only see this when you're looking at what is effectively closed-source Swift – you're probably using Xcode to look

at generated headers." Ref[2]

"The default is produced by Xcode and means "there's a default value specified, but you can't see what it is because

you don't have the source code." It's not syntactically valid to write this yourself. " Ref[2]

Ref

1. "default" keyword in function declarations?

https://www.reddit.com/r/swift/comments/4im0jb/default_keyword_in_function_declarations/

2. Default keyword in Swift parameter

http://stackoverflow.com/questions/24991791/default-keyword-in-swift-parameter

9. Variadic Parameters

可变的参数

"Variadic parameters are simply a more readable version of passing in an array of elements. In fact,

if you were to look at the type of the internal parameter names in the below example, you’d see

that it is of type [String] (array of strings):"

 func helloWithNames(names: String...) { // A: names's type is [String]
for name in names {
println("Hello, \(name)")
}
} // 2 names
helloWithNames("Mr. Robot", "Mr. Potato")
// Hello, Mr. Robot
// Hello, Mr. Potato // 4 names
helloWithNames("Batman", "Superman", "Wonder Woman", "Catwoman")
// Hello, Batman
// Hello, Superman
// Hello, Wonder Woman
// Hello, Catwoman

Ref

1. The Many Faces of Swift Functions

https://www.objc.io/issues/16-swift/swift-functions/#variadic-parameters

最新文章

  1. Ubuntu下配置apache开启https
  2. telnet模拟邮件发送
  3. 自己写的几个android自定义组件
  4. servlet/filter/listener/interceptor区别与联系
  5. tttttabs
  6. c# 反射学习笔记
  7. suds 在python3.x上的安装并访问webservice
  8. 多线程学习之一独木桥模式Single Threaded Execution Pattern
  9. POJ 3667 &amp; 1823 Hotel (线段树区间合并)
  10. 创建naarray(1)
  11. Android --- 读取系统资源函数getResources()小结
  12. Spring自定义标签
  13. Struts2基础学习(六)&mdash;文件的上传和下载
  14. python 将验证码保存到本地 读取 写入
  15. Nginx防盗链配置案例配置,Nginx的知识分享
  16. 浮动和BFC的学习整理转述
  17. 关闭默认共享,禁止ipc$空连接
  18. &lt;!特别的一天&gt;
  19. [Swift]LeetCode294. 翻转游戏之 II $ Flip Game II
  20. 详解javascript中this的工作原理

热门文章

  1. Robot Framework 自动化框架大纲
  2. JAVA体系学习-导向
  3. docker:学习笔记
  4. LoadRunner 安装汉化后的一些问题
  5. 关于c# Debug和Release的区别 (转)
  6. MATLAB 出一张好看的图
  7. SQL表变量和临时表
  8. vue组件is属性详解
  9. php导出excel不使用科学计数法
  10. CSRF总结