Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 理解闭包语法_Swift - Fatal编程技术网

Swift 理解闭包语法

Swift 理解闭包语法,swift,Swift,我正在研究闭包,不知道为什么以下方法不起作用: let names = ["Scarlet", "Stan", "Mike", "Walter", "Jessie"] var sortedClosure : (_ : String, _ : String) -> Bool sortedClosure = { $0 > $1 } names.sort(by: sortedClosure) 错误是: 传递给不带参数的调用的参数 但按排序的函数声明如下: sorted(by: { (

我正在研究闭包,不知道为什么以下方法不起作用:

let names = ["Scarlet", "Stan", "Mike", "Walter", "Jessie"]
var sortedClosure : (_ : String, _ : String) -> Bool

sortedClosure = { $0 > $1 }

names.sort(by: sortedClosure)
错误是:

传递给不带参数的调用的参数

但按排序的函数声明如下:

sorted(by: { (s1: String, s2: String) -> Bool in }

要更改数组
名称
,必须将其设置为变量。以下代码有效:

var names = ["Scarlet", "Stan", "Mike", "Walter", "Jessie"]
var sortedClosure : (_ : String, _ : String) -> Bool   // declares closure
sortedClosure = { $0 > $1 }   // initializes closure
在此之后,您有两个选择:

  • 应用排序(按:)并将其分配给数组:

    names = names.sorted(by: sortedClosure)  // applies sorted(by:) and assigns the array to the newly created one
    
    names.sort(by: sortedClosure)
    
  • 应用
    排序(按:)
    ,这会改变数组:

    names = names.sorted(by: sortedClosure)  // applies sorted(by:) and assigns the array to the newly created one
    
    names.sort(by: sortedClosure)
    
当您第一次调用
sort(by:)
时,您收到了错误,因为您的
names
是一个let常量。由于
sort(by:)
会对数组进行变异,因此它必须是一个变量


然而,对于你想做的事,我个人不会这样做。对于仅按字母顺序“降序”排序的数组,您应该尝试以下方法:

names.sort(by: >)

要更改数组
名称
,必须将其设置为变量。以下代码有效:

var names = ["Scarlet", "Stan", "Mike", "Walter", "Jessie"]
var sortedClosure : (_ : String, _ : String) -> Bool   // declares closure
sortedClosure = { $0 > $1 }   // initializes closure
在此之后,您有两个选择:

  • 应用排序(按:)并将其分配给数组:

    names = names.sorted(by: sortedClosure)  // applies sorted(by:) and assigns the array to the newly created one
    
    names.sort(by: sortedClosure)
    
  • 应用
    排序(按:)
    ,这会改变数组:

    names = names.sorted(by: sortedClosure)  // applies sorted(by:) and assigns the array to the newly created one
    
    names.sort(by: sortedClosure)
    
当您第一次调用
sort(by:)
时,您收到了错误,因为您的
names
是一个let常量。由于
sort(by:)
会对数组进行变异,因此它必须是一个变量


然而,对于你想做的事,我个人不会这样做。对于仅按字母顺序“降序”排序的数组,您应该尝试以下方法:

names.sort(by: >)

什么是
名称
?(请提供a)是否有可能将其声明为
let
sort
对集合进行适当的变异,因此它需要是
var
。因此它被声明为
let
–将其更改为
var
@Hamish,因为该代码使用let变量名,为什么?names.sorted(按:{(s1:String,s2:String)->Bool返回s1>s2})
sorted(按:)
不改变集合,它返回一个新数组
let sorted=names。排序(按:sortedClosure)
也应该有效。什么是
名称
?(请提供a)是否有可能将其声明为
let
sort
对集合进行适当的变异,因此它需要是
var
。因此它被声明为
let
–将其更改为
var
@Hamish,因为该代码使用let变量名,为什么?names.sorted(按:{(s1:String,s2:String)->Bool返回s1>s2})
sorted(按:)
不改变集合,它返回一个新数组
let sorted=names.sorted(按:sortedClosure)
也应该可以。Xcode谢谢,names.sort(按:>)的确切含义是什么?当我们在by:之后传递“>”时,会调用什么闭包?它作为与sortedClosureXcode等效的东西传递。谢谢,名称的确切含义是什么。sort(by:>)?当我们在by之后传递“>”时,将调用什么样的闭包:?它作为与sortedClosure等价的东西传递