斯威夫特/ XCode中生成错误:无法将类型的价值[模具]预期参数类型[模具]
问题描述:
使用的XCode 8.3.3,我相信使用雨燕3.1,但不是100%确定(你怎么知道?)。这是完整的代码。请注意,当我把它粘贴到一个干净的游乐场时,我没有任何错误。但是,一个XCode项目中,我得到就行生成错误“让骰子=骰子(withArrayOfDie:arrayOfDie)”中的单元测试:斯威夫特/ XCode中生成错误:无法将类型的价值[模具]预期参数类型[模具]
let defaultFaceCount = 6
let defaultDieCount = 6
func randomInt(withMaxValue maxValue: Int) -> Int {
return Int(arc4random_uniform(UInt32(maxValue))) + 1
}
class Die
{
private let m_faceCount: Int // Constant only set in init
private var m_faceValue: Int?
init(numFaces initialFaceCount: Int, withValue initialFaceValue: Int) {
// Make sure number of faces is greater than 0.
m_faceCount = (initialFaceCount > 0) ? initialFaceCount : defaultFaceCount
// Make sure face value is in proper range.
if initialFaceValue == 0 || initialFaceValue > m_faceCount {
m_faceValue = randomInt(withMaxValue: m_faceCount)
}
else {
m_faceValue = abs(initialFaceValue)
}
}
convenience init(numFaces initialFaceCount: Int) {
self.init(numFaces: initialFaceCount,
withValue: randomInt(withMaxValue: initialFaceCount))
}
convenience init() {
self.init(numFaces: defaultFaceCount)
}
var faceValue: Int {
get {
return m_faceValue!
}
}
var faceCount: Int {
get {
return m_faceCount
}
}
func roll() {
// face values are 1 based!
m_faceValue = randomInt(withMaxValue: m_faceCount)
}
}
class Dice {
var m_dice: [Die]
var m_occurrencesOf: [Int]
// Init with a pre-initialized array of Die. Every Die in
// the array must have the same face count.
init(withArrayOfDie: [Die]) {
var faceCount = defaultFaceCount
if withArrayOfDie.isEmpty {
// If there are no dice, add defaults.
m_dice = [Die]()
for _ in 1...defaultDieCount {
m_dice.append(Die(numFaces: defaultFaceCount))
}
}
else {
m_dice = withArrayOfDie
faceCount = m_dice[0].faceCount
}
// Keep trace of # of occurrences of each face value.
m_occurrencesOf = Array(repeating: 0, count: faceCount)
for die in m_dice {
m_occurrencesOf[die.faceValue - 1] += 1
}
}
// Init numDice dice, each with numFaces.
convenience init(numDice count: Int, numFaces faceCount: Int) {
var dice = [Die]()
for _ in 1...count {
dice.append(Die(numFaces: faceCount))
}
self.init(withArrayOfDie: dice)
}
// Init defaultDieCount dice, each with defaultFaceCount faces.
convenience init() {
self.init(numDice: defaultDieCount, numFaces: defaultFaceCount)
}
var count: Int {
return m_dice.count
}
// Retrieve the die at the specified (0 based) index.
func die(atIndex index: Int) -> Die? {
if !m_dice.isEmpty && index >= 0 && index < m_dice.count {
return m_dice[index]
}
return nil
}
subscript(index: Int) -> Die? {
get {
return die(atIndex: index)
}
}
}
// Unit Test
var arrayOfDie = [Die]()
for i in 1...6 {
arrayOfDie.append(Die())
}
let dice = Dice(withArrayOfDie: arrayOfDie)
// XCTAssertEqual(6, dice.count)
我得到的编译错误“无法将类型的值模]设定为“上线‘预期参数类型[模具]让骰子=骰子(withArrayOfDie:arrayOfDie)’。无法弄清为什么作为die数组的参数与预期的init参数类型不匹配。
谢谢!
答
得到var m_dice: [Die]()
class Dice {
var m_dice: [Die]
init(withArrayOfDie dice: [Die]) {
m_dice = dice
}
}
+0
都尝试“变种m_dice = [模具]()”和“VAR m_dice:[模具]”,但仍然得到同样的错误两种方式。前者应该使m_dice总是引用一个有效的数组,后者只是使用类型注释。我认为前者我仍然可以在init()中修改m_dice。我认为后者我必须在init()中初始化m_dice。但是,无论如何,我仍然得到错误。感谢您的回应! – Ratso
摆脱
()
行'VAR m_dice:[模具]()'是无效的。摆脱'()'。 – rmaddy谢谢rmaddy。那是在原来的职位一个错字,代码实际上是 “VAR m_dice = [模具]()”,而不是 “VAR m_dice:[模具]()”。 (不是类型注释,0长度数组的初始分配)。 – Ratso
如果指定这将有助于其* *线给你生成错误。编辑:特别是因为我的代码复制/粘贴产生**没有**生成错误。 – dfd