gpt4 book ai didi

swift - 找不到 swift 类型的初始值设定项

转载 作者:行者123 更新时间:2023-11-28 09:01:20 25 4
gpt4 key购买 nike

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()))
}

error: swift:28:23: Cannot find an initializer for type 'Phone' that accepts an argument list of type '(name: String, pphone: String, NumOfDay: String, date: NSDate)'

为什么会出现此错误?

最佳答案

numOfDaydate 是您已经设置的常量。之后您将无法更改它们。只需使用:

PhoneP(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()))

关于swift - 找不到 swift 类型的初始值设定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31972877/

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