如何解决错误:“参数缺少参数”

时间:2019-07-14 03:25:51

标签: swift

我现在正在学习Udacity课程。 func screenVIP (age: Int, onGuestList: Bool, knowsTheOwner: Bool)以上的代码均由它们编写。我在下面写下了这些,但是“调用中缺少参数'knowsTheOwner'的参数”不断出现。

// Here are some variables to represent a person who wants to come in to the club
var name: String = "Ayush"
var age: Int = 19
var onGuestList: Bool = false
var knowsTheOwner: Bool = true

// Helper functions for admitting or denying entrance
func admit(person: String) {
    print("\(person), come and party with us!")
}

func deny(person: String) {
    print("Sorry, \(person), maybe you can go play Bingo with the Android team.")
}

func sendToOwner(knowsTheOwner: Bool, person: String) {
    if knowsTheOwner {
        print("\(person), buddy, come on in!")
    } else {
        print("Who is this joker?")
    }
}

// Functions to determine which clubgoers should be admitted
func screenVIP(age: Int, onGuestList: Bool, knowsTheOwner: Bool) {
    if  onGuestList && age >= 21 {
        admit(person: name)
    } else if knowsTheOwner{
        sendToOwner(person: name) // <-- Error here
    } else {
        deny(person: name)
    }
}
screenVIP(age: age, onGuestList: onGuestList, knowsTheOwner: knowsTheOwner)

1 个答案:

答案 0 :(得分:0)

有两种可能的解决方案:

1)为参数knowsTheOwner提供默认值。

    func sendToOwner(knowsTheOwner: Bool = true, person: String) {
    if knowsTheOwner {
        print("\(person), buddy, come on in!")
    } else {
        print("Who is this joker?")
    }
}

2)如果您不想为参数knowsTheOwner提供默认值,请执行以下操作:

 func screenVIP(age: Int, onGuestList: Bool, knowsTheOwner: Bool) {
    if  onGuestList && age >= 21 {
        admit(person: name)
    } else if knowsTheOwner{
        sendToOwner(knowsTheOwner: knowsTheOwner,person: name) // <-- knowsTheOwner here is the argument from above 
    } else {
        deny(person: name)
    }
}

您必须执行此操作,因为如错误所述,该参数没有默认值。它一定有一些价值。