无法将类型“<Int>”的值转换为预期的元素类型 <Any>
Cannot convert value of type '<Int>' to expected element type <Any>
我正在尝试学习 swift,但我有一个问题,我认为在 Java 中使用
我正在尝试构建一个记忆纸牌游戏,我有以下模型:
Theme.swift <- 负责为卡片建模不同类型的主题,想法是卡片可以有数字、图像等,这就是为什么它在名称后有一个通用类型
import Foundation
import UIKit
struct Theme<Type> {
internal init(name: String, emojis: [Type], numberOfPairs: Int, cardsColor: UIColor) {
self.name = name
self.emojis = emojis
if(numberOfPairs > emojis.count || numberOfPairs < emojis.count) {
fatalError("Index out of bounds")
}
self.numberOfPairs = numberOfPairs
self.cardsColor = cardsColor
}
var name: String
var emojis: [Type]
var numberOfPairs: Int
var cardsColor: UIColor
}
我还有一个负责游戏逻辑和卡片模型的游戏模型,我仍然需要实现很多东西,但这是代码
import Foundation
struct Game {
var themes: [Theme<Any>]
var cards: [Card<Any>]
var score = 0
var isGameOver = false
var choosenTheme: Theme<Any>
init(themes: [Theme<Any>]) {
self.themes = themes
self.choosenTheme = self.themes.randomElement()!
cards = []
for index in 0..\<choosenTheme.numberOfPairs {
cards.append(Card(id: index*2, content: choosenTheme.emojis[index]))
cards.append(Card(id: index*2+1, content: choosenTheme.emojis[index]))
}
}
mutating func endGame() {
isGameOver = true
}
mutating func penalizePoints() {
score -= 1
}
mutating func awardPoints () {
score += 2
}
struct Card<T>: Identifiable {
var id: Int
var isFaceUP: Bool = false
var content: T
var isMatchedUP: Bool = false
var isPreviouslySeen = false
}
}
如您所见,我使用 Any 类型来创建卡片和主题数组,因为它们可以包含字符串、数字或图像
在我的 ViewModel 中,我有以下代码,我试图用两个主题填充主题数组,一个是字符串类型的内容,另一个是 Int:
import Foundation
import SwiftUI
class GameViewModel {
static let halloweenTheme = Theme<Int>(name: "WeirdNumbers", emojis: [1, 2, 4, 9, 20, 30], numberOfPairs: 6, cardsColor: .darkGray)
static let emojisTheme = Theme<String>(name: "Faces", emojis: ["", "", "", "", "", "", "", ""], numberOfPairs: 5, cardsColor: .blue)
var gameController: Game = Game(themes: [halloweenTheme, emojisTheme])
}
但我不断收到此错误或类似错误:
Cannot convert value of type 'Theme<Int>' to expected element type
'Array<Theme<Any>>.ArrayLiteralElement' (aka 'Theme<Any>')
Cannot convert value of type 'Theme<String>' to expected element type
'Array<Theme<Any>>.ArrayLiteralElement' (aka 'Theme<Any>')
我的脑子快疯了,我想通过使用 [Theme] 我可以得到这样的数组:[Theme, Theme, Theme, ...] 但看起来不是
有人知道这里发生了什么吗?
您可以使用包装器结构,下面是基本示例。注意:如果你需要符合 Codable,你需要自己实现 encode/decode。
struct Values<A> {
let value: A
}
struct Wrapper {
let wrappedValue: Values<Any>
}
class MyClass {
var wrappedValues: [Wrapper] = [Wrapper(wrappedValue: Values(value: "hello")), Wrapper(wrappedValue: Values(value: 1))]
}
我正在尝试学习 swift,但我有一个问题,我认为在 Java 中使用
我正在尝试构建一个记忆纸牌游戏,我有以下模型:
Theme.swift <- 负责为卡片建模不同类型的主题,想法是卡片可以有数字、图像等,这就是为什么它在名称后有一个通用类型
import Foundation
import UIKit
struct Theme<Type> {
internal init(name: String, emojis: [Type], numberOfPairs: Int, cardsColor: UIColor) {
self.name = name
self.emojis = emojis
if(numberOfPairs > emojis.count || numberOfPairs < emojis.count) {
fatalError("Index out of bounds")
}
self.numberOfPairs = numberOfPairs
self.cardsColor = cardsColor
}
var name: String
var emojis: [Type]
var numberOfPairs: Int
var cardsColor: UIColor
}
我还有一个负责游戏逻辑和卡片模型的游戏模型,我仍然需要实现很多东西,但这是代码
import Foundation
struct Game {
var themes: [Theme<Any>]
var cards: [Card<Any>]
var score = 0
var isGameOver = false
var choosenTheme: Theme<Any>
init(themes: [Theme<Any>]) {
self.themes = themes
self.choosenTheme = self.themes.randomElement()!
cards = []
for index in 0..\<choosenTheme.numberOfPairs {
cards.append(Card(id: index*2, content: choosenTheme.emojis[index]))
cards.append(Card(id: index*2+1, content: choosenTheme.emojis[index]))
}
}
mutating func endGame() {
isGameOver = true
}
mutating func penalizePoints() {
score -= 1
}
mutating func awardPoints () {
score += 2
}
struct Card<T>: Identifiable {
var id: Int
var isFaceUP: Bool = false
var content: T
var isMatchedUP: Bool = false
var isPreviouslySeen = false
}
}
如您所见,我使用 Any 类型来创建卡片和主题数组,因为它们可以包含字符串、数字或图像
在我的 ViewModel 中,我有以下代码,我试图用两个主题填充主题数组,一个是字符串类型的内容,另一个是 Int:
import Foundation
import SwiftUI
class GameViewModel {
static let halloweenTheme = Theme<Int>(name: "WeirdNumbers", emojis: [1, 2, 4, 9, 20, 30], numberOfPairs: 6, cardsColor: .darkGray)
static let emojisTheme = Theme<String>(name: "Faces", emojis: ["", "", "", "", "", "", "", ""], numberOfPairs: 5, cardsColor: .blue)
var gameController: Game = Game(themes: [halloweenTheme, emojisTheme])
}
但我不断收到此错误或类似错误:
Cannot convert value of type 'Theme<Int>' to expected element type 'Array<Theme<Any>>.ArrayLiteralElement' (aka 'Theme<Any>')
Cannot convert value of type 'Theme<String>' to expected element type 'Array<Theme<Any>>.ArrayLiteralElement' (aka 'Theme<Any>')
我的脑子快疯了,我想通过使用 [Theme
有人知道这里发生了什么吗?
您可以使用包装器结构,下面是基本示例。注意:如果你需要符合 Codable,你需要自己实现 encode/decode。
struct Values<A> {
let value: A
}
struct Wrapper {
let wrappedValue: Values<Any>
}
class MyClass {
var wrappedValues: [Wrapper] = [Wrapper(wrappedValue: Values(value: "hello")), Wrapper(wrappedValue: Values(value: 1))]
}