无法在swift中找到类型的初始化程序

时间:2015-08-12 18:34:19

标签: swift

struct PhoneP {
    var name = "Un-Name"
    var pphone = "Un-Phone"
    let NumOfDay = ""
    let date :NSDate = NSDate()
}

class PhoneManager: NSObject {

    var phones = [PhoneP]()

    func addPhone(name: String, phone: String, Nday: String){
        phones.append(PhoneP(name: name, pphone: phone, NumDay: Nday ,date: NSData()))
        //phones.append(Phone(name: name, pphone: phone, NumOfDay: Nday, date: NSDate()))
}
  

错误:swift:28:23:找不到类型'Phone'的初始值设定项,它接受类型'(name:String,pphone:String,NumOfDay:String,date:NSDate)'的参数列表

为什么我会收到此错误?

1 个答案:

答案 0 :(得分:0)

datePhoneP(name: "name", pphone: "phone") 是您已设置的常量。之后您将无法更改它们。只需使用:

struct PhoneP {
    var name = "Un-Name"
    var pphone = "Un-Phone"
    let NumOfDay: String
    let date: NSDate
}

或者将您的结构更改为:

PhoneP(name: "name", pphone: "phone", NumOfDay: "day", date: NSDate())

然后您可以创建结构,如:

NSData()

此外,您正在将NSDate添加到phones.append(PhoneP(name: name, pphone: phone, NumDay: Nday ,date: NSData())) 的类型中,因此也不会编译。

resources :articles do
    resources :comments
end