gpt4 book ai didi

快速便利初始化程序错误 : must precede argument

转载 作者:行者123 更新时间:2023-11-28 12:35:19 27 4
gpt4 key购买 nike

我一直在尝试创建我的第一个便利初始化器。我收到此错误:

我收到错误:参数“valueInDollars”必须先于参数“serialNumber”

这是我的所有代码,我的问题在代码的底部,但我认为显示所有内容很有用。

import UIKit

class Item: NSObject {

var name: String
var valueInDollars: Int
var serialNumber: String?
let dateCreated: NSDate



init(name: String, valueInDollars: Int, serialNumber: String?) {

self.name = name
self.valueInDollars = valueInDollars
self.serialNumber = serialNumber
self.dateCreated = NSDate()

super.init()

}

//create a random item:

convenience init(random: Bool = false) {

if random {

let adjectives = ["Fluffy", "Rusty", "Bluffy"]
let nouns = ["Boon", "Noon", "Toon",]

var idx = arc4random_uniform(UInt32(adjectives.count))
let randomAdjective = adjectives[Int(idx)]

idx = arc4random_uniform(UInt32(nouns.count))
let randomNoun = nouns[Int(idx)]


let randomName = "\(randomAdjective) \(randomNoun)"

let randomValue = Int(arc4random_uniform(100))

let randomSerialNumber = NSUUID().UUIDString.componentsSeparatedByString("-").first!


self.init(name: randomName, serialNumber: randomSerialNumber, valueInDollars: randomValue)
}
else {

self.init(name: "", serialNumber: nil, valueInDollars: 0)



}
}
}

最佳答案

您应该使用定义 init 本身时使用的相同顺序的参数来调用 init

self.init(name: "", valueInDollars: 0, serialNumber: nil)

关于快速便利初始化程序错误 : must precede argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41041627/

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