inferred to have type (), which may be unexpected"?-6ren"> inferred to have type (), which may be unexpected"?-我正在尝试对类的实例化项目数组进行排序:“Employees”。 但是我在 Playground 上遇到以下错误: 这是在我的 Playground 中编写的代码: class Employee { -6ren">
gpt4 book ai didi

sorting - 为什么会出现警告 : "Constant inferred to have type (), which may be unexpected"?

转载 作者:行者123 更新时间:2023-12-02 15:25:00 25 4
gpt4 key购买 nike

我正在尝试对类的实例化项目数组进行排序:“Employees”。

但是我在 Playground 上遇到以下错误:

enter image description here

这是在我的 Playground 中编写的代码:

class Employee {
var firstName:String = ""
var lastName:String = ""
init(fName:String, lName:String) {
self.firstName = fName
self.lastName = lName
}
}

var employees = [Employee]()

// 1)
var employee = Employee(fName: "Ric", lName: "Lee")
employees.append(employee)

// 2)
employee = Employee(fName: "Ralph", lName: "Knott")
employees.append(employee)

// 3)
employee = Employee(fName: "Joe", lName: "Smirf")
employees.append(employee)

// 4)
employee = Employee(fName: "Meredith", lName: "Lind")
employees.append(employee)

// 5)
employee = Employee(fName: "Aarnald", lName: "Zingerhost")
employees.append(employee)

let sortedEmployees = employees.sort { (e1:Employee, e2:Employee) -> Bool in
e1.lastName < e2.lastName
}

我在这里缺少什么?
为什么会出现警告?

最佳答案

原因是您使用了错误的函数(以前的sortInPlace):

Swift 3

  • sort() 已重命名为 sorted()
  • sortInPlace() 已重命名为 sort()

所以是

let sortedEmployees = employees.sorted { (e1:Employee, e2:Employee) -> Bool in
e1.lastName < e2.lastName
}

来源:Swift Evolution: Apply API Guidelines to the Standard Library

关于sorting - 为什么会出现警告 : "Constant <name> inferred to have type (), which may be unexpected"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38272625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com