1
私はプロトコルを作成して、単純なチェーンのオブジェクトを先行オブジェクトと後継オブジェクトとリンクさせることができます。コアデータクラスをプロトコル「自己」の要件を満たすために最終的にする
私はコアデータのエンティティ「イベント」で、このプロトコルを使用しようとしているが、私はこのエラーを取得しています:
エラー:
Protocol 'Chainable' requirement 'chain' cannot be satisfied by a non-final class ('Event') because it uses 'Self' in a non-parameter, non-result type position.
どのように私は(このクラスfinal
を作ることができます私はマニュアルにCodegenを設定したくない)、またはvarチェーンを書き直す:[Self]
定義?
ありがとうございます。
extension Event: Chainable {}
protocol Chainable where Self: NSManagedObject {
var chain: [Self] { get }
var predecessor: Self? { get set }
var successor: Self? { get set }
var selfIndexInChain: Int { get }
mutating func moveInChain(to index: Int)
mutating func removeSelfFromChain()
mutating func addSelfAsSuccessor(of object: Self)
mutating func addSelfAsPredecessor(of object: Self)
}
extension Chainable {
var chain: [Self] {
var _chain: [Self] = [self]
// go right
var current: Self = self
while let successor = current.successor {
_chain.append(successor)
current = successor
}
// go left
current = self
while let predecessor = current.predecessor {
_chain.insert(predecessor, at: 0)
current = predecessor
}
// TODO: - Compare speed with the alternative realization: Find the first element, then populate the tasks array.
return _chain
}
// Self Index in the chain
var selfIndexInChain: Int { return self.chain.index(of: self)! }
// Change place in the cahain
mutating func moveInChain(to index: Int) {
guard index != selfIndexInChain else { return } // Check the index isn't same
guard 0...(chain.count-1) ~= index else { return }
let taskAtDestination = chain[index]
if index > selfIndexInChain {
removeSelfFromChain()
addSelfAsSuccessor(of: taskAtDestination)
} else {
removeSelfFromChain()
addSelfAsPredecessor(of: taskAtDestination)
}
}
mutating func removeSelfFromChain() {
let successor = self.successor
let predecessor = self.predecessor
self.predecessor = nil
self.successor = nil
self.predecessor?.successor = successor
self.successor?.predecessor = predecessor
}
mutating func insertSelfBetween(lhs: Self, rhs: Self) {
// self shouldn't be linked
guard self.predecessor == nil && self.successor == nil else { return }
guard (lhs.successor == rhs && rhs.predecessor == lhs) ||
(lhs.successor == nil && rhs.predecessor == nil) else { return } // If both are nil, they will be connected thru self
self.predecessor = lhs
self.successor = rhs
}
mutating func addSelfAsSuccessor(of object: Self) {
// self shouldn't be linked
guard self.predecessor == nil && self.successor == nil else { return } // TODO: Add error support
// self shouldn't be already the successor
guard object.successor != self else { return }
let previousSuccessor = object.successor
self.predecessor = object
self.successor = previousSuccessor
}
mutating func addSelfAsPredecessor(of object: Self) {
// self shouldn't be linked
guard self.predecessor == nil && self.successor == nil else { return }
// self shouldn't be the task successor already
guard object.predecessor != self else { return }
let previousPredecessor = object.predecessor
self.successor = object
self.predecessor = previousPredecessor
}
}