Swift协议定义为空:
public protocol CVarArgType {
}
Run Code Online (Sandbox Code Playgroud)
Apple文档页面未列出任何必需的方法:https : //developer.apple.com/library/ios/documentation/Swift/Reference/Swift_CVarArgType_Protocol/index.html
因此,我希望这可以工作:
extension String : CVarArgType {
}
Run Code Online (Sandbox Code Playgroud)
但出现构建错误:协议要求属性'_cVarArgEncoding'具有类型[[Int]'(Swift.CVarArgType)
鉴于协议定义为空,此要求从何而来?
如果我实现了计算属性,请继续前进:
extension String : CVarArgType {
public var _cVarArgEncoding: [Int] {
get {
//What is expected to be returned here?
}
}
}
Run Code Online (Sandbox Code Playgroud)
预期将以数组形式返回Int什么?
更新:为什么我需要这个?
我有一个名为Identifiable的协议,我的核心数据实体模型类符合该协议,我对此协议进行了扩展,并带有几个约束,以提供一个使用带有格式构造函数的NSPredicate中的id值的函数,该函数需要CVarArgType。
public protocol Identifiable {
typealias IdentityType: CVarArgType, Hashable
var id: IdentityType { get }
}
extension Identifiable where Self: Findable, Self: NSManagedObject {
static public func find(id: IdentityType, context: NSManagedObjectContext) -> Self? {
return find(NSPredicate(format: "id = %@", id), context: context)
}
}
public extension Findable where Self: NSManagedObject {
static public func find(predicate: NSPredicate?, context: NSManagedObjectContext) throws -> Self? {
let fetchRequest = fetchRequestForEntity(inContext: context)
fetchRequest.predicate = predicate
fetchRequest.fetchLimit = 1
return try context.executeFetchRequest(fetchRequest).first as? Self
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2219 次 |
| 最近记录: |