Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 当我尝试调用self.init时,便利初始值设定项有什么问题?_Swift_Initialization_Convenience Methods - Fatal编程技术网

Swift 当我尝试调用self.init时,便利初始值设定项有什么问题?

Swift 当我尝试调用self.init时,便利初始值设定项有什么问题?,swift,initialization,convenience-methods,Swift,Initialization,Convenience Methods,我写了下面的代码。当我尝试调用self.init时,我得到的错误是在便利初始值设定项的末尾。我的逻辑或语法有什么问题?或者我该如何调试它?Xcode给出的错误是“无法使用类型为的参数列表调用” 谢谢你在这方面的帮助 import Foundation import UIKit class Item: NSObject { var name: String var valueInDollars: Int var serialNumber: String? let

我写了下面的代码。当我尝试调用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:String,被盗:Bool)'

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

所以你需要更换这条线

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语句,因为当随机参数为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)
        }
    }

查看您的
init
方法。您忘记传递一个参数。1。您的答案解决了在问题中发布的代码中修复问题后将发生的新错误。2.不要在Swift中使用
NSDate
,使用
Date
@rmaddy 1。错误是什么?2.NSDate在指定的初始值设定项中使用,为了避免强制转换,我使用了NSDate初始化,我同意您的看法,使用NSDate是没有理由的。问题中的错误是在调用便利初始值设定项中的self.init(…)时传递了错误的参数。@CosmicArrows
NSDate
(以及许多其他
NS*
类)是针对Objective-C的。Swift提供
Date
(以及许多其他非NS类/结构替换)。@CosmicArrows请阅读Swift手册中的章节。重点关注“类继承和初始化”部分。方便的初始化者必须调用
self
初始化者。