Swift" Call中的额外争论"将.append用于类数组时出错

时间:2016-09-05 09:41:21

标签: ios swift mobile

我刚刚开始学习Swift,作为我的一个程序的一部分,我一直在尝试维护一个类实例数组并使用.append将新实例添加到类数组中。

但是,当尝试将新的类实例附加到数组时," Call中的额外参数"出现错误。我一直确定为所有变量指定数据类型,以确保没有与数据类型冲突的编译器错误,但这仍然没有解决问题。

以下是代码:

import UIKit

var personMgr: personManager = personManager()

class person{

    var name:String = "Default"
    var description:String = "Default"
    var presentIdeasDict:[Int: String] = [
        0: "nil"
    ]
    var presentLinkDict:[Int: String] = [       //Use same key for name of present idea and link for the present
    0: "nil"
    ]

}

class personManager{

    var people = [person]()

    func addPerson(name: String, description: String){
        people.append(person(name: name, description: description, presentIdeasDict: [0: "nil"], presentLinkDict: [0: "nil"]))
    }

}

错误表示"额外参数' name'在电话中排队:

people.append(person(name: name, description: description, presentIdeasDict: [0: "nil"], presentLinkDict: [0: "nil"]))

1 个答案:

答案 0 :(得分:1)

您需要为您的人员制作初始化程序"类。

请注意,您也可以为初始化程序参数设置默认值。这样你就不必在几个地方使用默认值(你甚至可以省略其他初始值设定项中的默认参数)。

另请注意,按照惯例,类名应大写。

示例:

class Person {

    var name:String
    var description:String
    var presentIdeasDict:[Int: String]
    var presentLinkDict:[Int: String]

    init(name: String = "Default", description: String = "Default", presentIdeasDict: [Int: String] = [0: "nil"], presentLinkDict: [Int: String] = [0: "nil"]) {
        self.name = name
        self.description = description
        self.presentIdeasDict = presentIdeasDict
        self.presentLinkDict = presentLinkDict
    }

}

class PersonManager {

    var people = [Person]()

    func addPerson(name: String, description: String) {
        people.append(Person(name: name, description: description))
    }

}