将核心数据类作为最终类以满足协议“自我”要求
Posted
技术标签:
【中文标题】将核心数据类作为最终类以满足协议“自我”要求【英文标题】:Making the Core Data class as final to satisfy the protocol 'Self' requirement 【发布时间】:2017-11-28 20:18:18 【问题描述】:我编写了一个协议,允许将简单链中的对象与前任和后继联系起来。
我正在尝试将此协议与我的核心数据实体“事件”一起使用,但出现此错误:
错误:
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 chain: [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
【问题讨论】:
【参考方案1】:我也遇到了类似的问题。
解决方法:
查看您的代码,Chainable
似乎将由多个 NSManagedObject
子类实现。
用Chainable
代替Self
并让Chainable
包含需要与NSManagedObject
子类通用的最低要求。
在某些情况下,这意味着为 NSManagedObject
子类实现的某些功能实现包装器
【讨论】:
以上是关于将核心数据类作为最终类以满足协议“自我”要求的主要内容,如果未能解决你的问题,请参考以下文章