好吧,所以我练习嵌套类型,而且我不确定我在哪里出错了,它可能非常简单,但它不是现在来找我,所以我想我会寻求帮助。我一直在<<在创建 Employee 类的intance时,错误类型>> 。有小费吗?
class Employee {
var name: String
var profession: Profession
enum Profession {
var salary = 0
case .Developer, .Engineer, .QA, .ProjectLead
func averageSalary() {
switch self {
case .Developer:
salary = 85_000
case .Engineer:
salary = 90_000
case .QA:
salary = 70_000
case .ProjectLead:
salary = 90_000
}
println("Average Salary: \(salary)")
}
}
init(name: String, profession: Profession) {
self.name = name
self.profession = profession
}
}
let c = Employee(name: "Justin", profession: Employee.Profession.Developer)
答案 0 :(得分:1)
错误在枚举中:
枚举可能不包含存储的属性
(工资变量)
并且你不应该在案例名称之前写一个点:
enum Profession {
case Developer, Engineer, QA, ProjectLead
func averageSalary() -> Int {
switch self {
case Developer:
return 85_000
case Engineer:
return 90_000
case QA:
return 70_000
case ProjectLead:
return 90_000
}
}
}
答案 1 :(得分:1)
您的Profession
枚举存在一些问题。
首先,枚举不能存储属性,例如salary
。如果符合您的需要,您可以使用相关值。请参阅:Swift Language Reference: Enumerations - Associated Values
否则,对于averageSalary
func,我只会将新的平均工资作为Int
其次,你不应该在每个案例声明的开头包括点。所以:
case Developer, Engineer, QA, ProjectLead