当我尝试调用self.init时,我的便利性初始化器有什么问题



我写了下面的代码。当我尝试调用self.init时,我遇到的错误是在便利性初始化器的结尾处。我的逻辑或语法怎么了?还是我该如何调试?Xcode的错误是"无法使用类型的参数列表调用"。

感谢您对此的任何帮助

import Foundation
import UIKit
class Item: NSObject {
    var name: String
    var valueInDollars: Int
    var serialNumber: String?
    let dateCreated: NSDate
    var stolen: Bool?
    init(name: String, valueInDollars: Int, serialNumber: String?, dateCreated: NSDate, stolen: Bool?) {
        self.name = name
        self.valueInDollars = valueInDollars
        self.serialNumber = serialNumber
        self.dateCreated = NSDate()
        self.stolen = stolen
        //below why did I have to call super.init for this custom class that inherits from NSObject? Doesn't it automatically get called anyway once the custom object is initialized since it inherits from NSObject?  It just seems like more work on my behalf which isn't fair.  it should do it automatically.  Why wouldn't it do it automatically if it inherits from NSObject?
        super.init()
    }
    convenience init(random: Bool = false) {
        if random {
            let adjectives = ["Fluffy", "Rusty", "Shiny"]
            let nouns = ["MacBook Pro", "Red Tribe Bike", "Vegan Pizzas"]
            //take a variable that's random; the highest value for this random number will be the number of ojbects in the adjectives array
            var idx = arc4random_uniform(UInt32(adjectives.count))
            //now use this random variable and let it be the index of the adjectives array...so basically it'll be a random object from the adjectives array
            let randomAdjective = adjectives[Int(idx)]
            //AWESOME!! Now that the random adjective is stored in the randomAdjective constant, let's re-use the idx variable...Ayyyyeeeee re-use!
            //we'll re-use it by doing the same process or close to the same process for nouns
            idx = arc4random_uniform(UInt32(nouns.count))
            let randomNoun = nouns[Int(idx)]
            //now let's concatenate these two clever words, shall we!!
            let randomName = "(randomAdjective) (randomNoun)"
            //yayyy we're programmmminnngg!
            //now let's ....whad de fuk....
            let randomValue = Int(arc4random_uniform(100))
            let randomSerialNumber = NSUUID().uuidString.components(separatedBy: "-").first!
            let betterNotBeStolen: Bool = false
            self.init(name: randomName, valueInDollars: randomValue, serialNumber: randomSerialNumber, stolen: betterNotBeStolen)

        }
    }
}

您有错误

"用类型的参数列表'无法调用'item.init'(名称: 字符串,valueindollars:int,serialnumber:字符串,被盗:bool('

因为您错过了self.init(params ...(。

的datecreated param。

因此您需要替换此行

self.init(name: randomName, valueInDollars: randomValue, serialNumber: randomSerialNumber, stolen: betterNotBeStolen)

使用这个

self.init(name: randomName, valueInDollars: randomValue, serialNumber: randomSerialNumber,dateCreated: NSDate(), stolen: betterNotBeStolen)

您将在

之后看到的下一个错误

self.init在从initializer返回之前并未在所有路径上调用

因此,您需要添加其他语句,因为初始化器不知道随机参数为false时该怎么做。

convenience init(random: Bool = false) {
        if random {
            let adjectives = ["Fluffy", "Rusty", "Shiny"]
            let nouns = ["MacBook Pro", "Red Tribe Bike", "Vegan Pizzas"]
            //take a variable that's random; the highest value for this random number will be the number of ojbects in the adjectives array
            var idx = arc4random_uniform(UInt32(adjectives.count))
            //now use this random variable and let it be the index of the adjectives array...so basically it'll be a random object from the adjectives array
            let randomAdjective = adjectives[Int(idx)]
            //AWESOME!! Now that the random adjective is stored in the randomAdjective constant, let's re-use the idx variable...Ayyyyeeeee re-use!
            //we'll re-use it by doing the same process or close to the same process for nouns
            idx = arc4random_uniform(UInt32(nouns.count))
            let randomNoun = nouns[Int(idx)]
            //now let's concatenate these two clever words, shall we!!
            let randomName = "(randomAdjective) (randomNoun)"
            //yayyy we're programmmminnngg!
            //now let's ....whad de fuk....
            let randomValue = Int(arc4random_uniform(100))
            let randomSerialNumber = NSUUID().uuidString.components(separatedBy: "-").first!
            let betterNotBeStolen: Bool = false
            self.init(name: randomName, valueInDollars: randomValue, serialNumber: randomSerialNumber,dateCreated: NSDate(), stolen: betterNotBeStolen)

        } else {
           self.init(name: "SomeName", valueInDollars: 3, serialNumber: "123", dateCreated: NSDate(), stolen: true)
        }
    }

最新更新