当我尝试调用self.init时,我的便利初始化程序出了什么问题?

时间:2018-04-26 22:40:43

标签: swift initialization convenience-methods

我写了下面的代码。当我尝试调用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)


        }
    }
}

1 个答案:

答案 0 :(得分:1)

您收到了错误

  

“无法使用类型'的参数列表调用'Item.init'(名称:   String,valueInDollars:Int,serialNumber:String,stolen:Bool)'“

因为你错过了self.init中的dateCreated参数(params ...)。

所以你需要替换这一行

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

这一个

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

您将看到的下一个错误是

  

在从初始化程序

返回之前,不会在所有路径上调用Self.init

所以你需要添加else语句,因为初始化程序在随机参数为假时不知道该怎么做。

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