我知道通常情况下我不能实例化一个协议。 但是如果我在协议中包含一个初始化器,那么编译器肯定知道当协议稍后被结构体或类使用时,它将有一个可以使用的init吧? 我的代码如下,其中包含以下行:
protocol Solution {
var answer: String { get }
}
protocol Problem {
var pose: String { get }
}
protocol SolvableProblem: Problem {
func solve() -> Solution?
}
protocol ProblemGenerator {
func next() -> SolvableProblem
}
protocol Puzzle {
var problem: Problem { get }
var solution: Solution { get }
init(problem: Problem, solution: Solution)
}
protocol PuzzleGenerator {
func next() -> Puzzle
}
protocol FindBySolvePuzzleGenerator: PuzzleGenerator {
var problemGenerator: ProblemGenerator { get }
}
extension FindBySolvePuzzleGenerator {
func next() -> Puzzle {
while true {
let problem = problemGenerator.next()
if let solution = problem.solve() {
return Puzzle(problem: problem, solution: solution)
}
}
}
}
这行代码:
return Puzzle(problem: problem, solution: solution)
出现错误:协议类型“Puzzle”无法实例化。