有多少种方法可以迅速编写闭包? [英] How many ways there to write a closure in swift?

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

问题描述

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

解决方案




关闭规则:








  1. 如果闭包仅在'in'关键字之后返回值(或
    闭合块仅返回一个值),然后使用'return'关键字
    可选。




  2. 如果明确定义了闭包类型
    ,则定义闭包参数类型是可选的。




  3. 如果闭包参数为<,则定义闭包返回类型是可选的/ em>
    定义了它的类型,并且在返回中没有其他语句
    块或闭包类型已明确定义。




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




  5. 定义闭包类型,参数,返回类型和'in'关键字是
    如果闭包中没有参数并且没有其他语句,则是可选的
    在返回块中。




  6. 明确定义闭包类型为如果闭包参数(如果
    any)是用其类型定义的,并且返回块中没有其他语句
    是可选的,则为可选




  7. 如果类型








实施中的关闭规则:

  //规则# 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:


  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.

  2. Defining closure argument type is optional if the closure type is explicitly defined.

  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.

  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.

  5. Defining closure type, arguments, return type and 'in' keyword is optional if closure have no arguments and there is no other statement in the return block.

  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.

  7. If type is defined explicitly then it should be defined completely.


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:

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

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