両方、迅速3を使用してXCodeの8.2ベータ版と迅速なビルドCLI経由して、私は私がhttps://hugotunius.se/2016/07/17/implementing-a-linked-list-in-swift.html一般的なリンクリストの実装でシーケンスプロトコル拡張を使用するSwift 3の問題。
@報告されたエラーを発見したリンクリストの実装を示す遊び場をコンパイルしようとしていますが次のとおりです。エラー:値を変換することはできませんタイプ 'Node?'の予想される引数型 'Node < _>?'
イテレータ拡張でエラーが発生します。 makeIterator関数のコンストラクタの呼び出しはエラー行です。
戻りLinkedListIterator(startNode:開始)
私は無駄に許容される型にstartNodeを強制するためのさまざまな方法を試みました。私はこれをどのように訂正するべきか、そしてなぜ重要なのはなぜそれが最初のエラーであるのかを知りたいのです。前もって感謝します。
関連するコード:
/* Node */
public class Node<T: Equatable> {
typealias NodeType = Node<T>
/// The value contained in this node
public let value: T
var next: NodeType? = nil
var previous: NodeType? = nil
public init(value: T) {
self.value = value
}
}
/* Linked List */
public final class LinkedList<T: Equatable> {
public typealias NodeType = Node<T>
fileprivate var start: NodeType? {
didSet {
// Special case for a 1 element list
if end == nil {
end = start
}
}
}
fileprivate var end: NodeType? {
didSet {
// Special case for a 1 element list
if start == nil {
start = end
}
}
}
/// The number of elements in the list at any given time
public fileprivate(set) var count: Int = 0
/// Wether or not the list is empty. Returns `true` when
/// count is 0 and `false` otherwise
public var isEmpty: Bool {
get {
return count == 0
}
}
/// Create a new LinkedList
///
/// - returns: An empty LinkedList
public init() {
}
/// Create a new LinkedList with a sequence
///
/// - parameter: A sequence
/// - returns: A LinkedList containing the elements of the provided sequence
public init<S: Sequence>(_ elements: S) where S.Iterator.Element == T {
for element in elements {
append(value: element)
}
}
}
public struct LinkedListIterator<T: Equatable>: IteratorProtocol {
public typealias Element = Node<T>
/// The current node in the iteration
private var currentNode: Element?
private init(startNode: Element?) {
currentNode = startNode
}
public mutating func next() -> LinkedListIterator.Element? {
let node = currentNode
currentNode = currentNode?.next
return node
}
}
extension LinkedList: Sequence {
public typealias Iterator = LinkedListIterator<T>
public func makeIterator() -> LinkedList.Iterator {
return LinkedListIterator(startNode: start)
}
}
は非常に多くのハミッシュ、ありがとうございました。私はすでに保護レベルをコード内の他の場所から保護するように変更していたが、メッセージの性質によって放棄されたので、後天的には明らかであるようだ。私はチャンスがあるとすぐにあなたの提案された修正を試み、報告を返すでしょう。 – Chris
実際にinitメソッドの保護レベルが問題でした。再度ありがとうHamish! – Chris
@Chrisを助けて幸い:) – Hamish