本文介绍了有多少种方法可以迅速编写闭包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:在迅速编写任何闭包时需要考虑哪些基本规则和边界(就语法而言)?

解决方案




关闭规则:











  1. 定义闭包参数,返回类型和'in'关键字是可选的
    如果显式定义了闭包,在这种情况下,如果闭包具有任何
    参数,必须使用它们并使用索引对其进行引用,
    例如:$ 0代表第一个参数,$ 1代表第二个参数,所以
    上。











实施中的关闭规则:

  //规则# 1:如果闭包块仅在 in关键字之后返回值(或者闭包块仅返回值),则使用 return关键字是可选的。 
var rule_1:(String)-> String = {(txt:String)->
中的字符串 Rule_1:'return'关键字未定义,因为在返回块中没有其他语句,Text = \(txt)
}
print(rule_1( xyz))
print()

//规则#2:如果显式定义了闭包类型,则定义闭包参数类型是可选的。
var rule_2:(String)-> String = {(txt)->
中的字符串print(,终止符:)
返回 Rule_2:闭包参数类型为未定义,因为显式定义了闭合类型,Text = \(txt)
}
print(rule_2( xyz))
print()

//规则#3:定义闭包返回类型是可选的,如果闭包参数是用闭包的类型定义的,并且在返回块中没有其他语句,或者闭包类型已明确定义。
var rule_3_1:(String)-> String = {(txt:String)in
print(,终止符:)
return Rule_3_1:未定义返回类型,因为闭包类型已明确定义,Text = \(txt)
}
var rule_3_2 = {$ txt $ String():
返回 Rule_3_2:未定义返回类型,因为参数定义为它是类型,并且返回块中没有其他语句,Text = \(txt)
}
print(rule_3_1( xyz))
print(rule_3_2( xyz ))
print()

//规则#4:如果显式定义了闭包,则定义闭包参数,返回类型和'in'关键字是可选的,在这种情况下,如果闭包有任何参数,则必须使用它们并使用索引对其进行引用,例如:$ 0表示第一个参数,$ 1表示第二个参数,依此类推。
var rule_4:(String)-> String = {
print(,结束符:)
return Rule_4:闭包参数,返回类型和'in'关键字不是定义,因为明确定义了闭包,Text = \($ 0)
}
print(rule_4( xyz))
print()

// Rule#5:如果闭包没有参数并且在return块中没有其他语句,则定义闭包类型(显式),参数,返回类型和'in'关键字是可选的。
var rule_5 = {
return Rule_5:闭包类型,闭包参数,返回类型和'in'关键字未定义,因为闭包没有参数,并且返回块中没有其他语句,Text = \ \ \
}
print(rule_5())
print()

//规则6:明确定义闭包类型是可选的如果闭包参数(如果有)是用其类型定义的,并且在return块中没有其他语句或返回类型也已定义。
var rule_6_1 = {(txt:String)->
中的字符串print(,终止符:)
返回 Rule_6_1:未明确定义闭包,因为闭包参数定义为它的类型和返回类型,Text = \(txt)
}
var rule_6_2 = {(txt:String)in
返回 Rule_6_2:未明确定义闭包,因为定义了闭包参数类型,并且返回块中没有其他语句,Text = \(txt)
}
print(rule_6_1( xyz))
print(rule_6_2( xyz ))






输出:




Question: What are the basic rules and boundaries (in terms of syntax) that need to be considered when writing any closure in swift?

解决方案


Closure Rules:



Closure rules in action:

// Rule#1: If the closure block solely returns value after the 'in' keyword (or the closure block just returns a value) then using 'return' keyword is optional.
var rule_1:(String)->String = { (txt:String)->String in
"Rule_1: 'return' keyword is not defined because there is no other statement in the return block, Text = \(txt)"
}
print(rule_1("xyz"))
print("")

// Rule#2: Defining closure argument type is optional if the closure type is explicitly defined.
var rule_2:(String)->String = { (txt)->String in
    print("", terminator:"")
    return "Rule_2: Closure argument type is not defined because closure type is explicitly defined, Text = \(txt)"
}
print(rule_2("xyz"))
print("")

// Rule#3: Defining closure return type is optional, if closure arguments are defined with it's type and there is no other statement in the return block or the closure type is explicitly defined.
var rule_3_1:(String)->String = { (txt:String) in
    print("", terminator:"")
    return "Rule_3_1: Return type is not defined because closure type is explicitly defined, Text = \(txt)"
}
var rule_3_2 = { (txt:String) in
    return "Rule_3_2: Return type not defined because arguments are defined with it's type and there is no other statement in the return block, Text = \(txt)"
}
print(rule_3_1("xyz"))
print(rule_3_2("xyz"))
print("")

// Rule#4: Defining closure arguments, return type and 'in' keyword is optional if closure is explicitly defined, in this case if the closure has any argument, it is compulsory to use them and refer them using indexes, eg: $0 represents first argument, $1 represents second argument and so on.
var rule_4:(String)->String = {
    print("", terminator:"")
    return "Rule_4: Closure arguments, return type and 'in' keyword is not defined because closure is explicitly defined, Text = \($0)"
}
print(rule_4("xyz"))
print("")

// Rule#5: Defining closure type(explicitly), arguments, return type and 'in' keyword is optional if closure have no arguments and there is no other statement in the return block.
var rule_5 = {
    return "Rule_5: Closure type, closure arguments, return type and 'in' keyword not defined because closure have no arguments and there is no other statement in the return block, Text = \"\""
}
print(rule_5())
print("")

// Rule#6: Defining closure type explicitly is optional if closure arguments(if any) are defined with it's type and either there is no other statement in the return block or return type is also defined.
var rule_6_1 = { (txt:String)->String in
    print("", terminator:"")
    return "Rule_6_1: Closure not explicitly defined because closure arguments are defined with it's type and return type, Text = \(txt)"
}
var rule_6_2 = { (txt:String) in
    return "Rule_6_2: Closure not explicitly defined because closure arguments are defined with it's type and there is no other statement in the return block, Text = \(txt)"
}
print(rule_6_1("xyz"))
print(rule_6_2("xyz"))


Output:

这篇关于有多少种方法可以迅速编写闭包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 08:21