【发布时间】:2016-03-16 20:08:43
【问题描述】:
Swift 协议定义为空:
public protocol CVarArgType {
}
Apple 文档页面没有列出任何必需的方法: https://developer.apple.com/library/ios/documentation/Swift/Reference/Swift_CVarArgType_Protocol/index.html
所以我希望这会起作用:
extension String : CVarArgType {
}
但我得到一个构建错误:Protocol requires property '_cVarArgEncoding' with type '[Int]' (Swift.CVarArgType)
鉴于协议定义为空,此要求从何而来?
如果我实现计算属性,则继续前进:
extension String : CVarArgType {
public var _cVarArgEncoding: [Int] {
get {
//What is expected to be returned here?
}
}
}
预计会以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
}
}
【问题讨论】:
-
我认为这没什么实用价值,有更好的方法来处理
va_list参数。但是,我认为这个问题仍然有一些优点,因为它探索了 ObjectiveC-Swift 集成的边缘。编码可以参考ObjectiveC type encoding。如果你能解释你想做什么,我们可以提供更多帮助 -
@CodeDifferent 我已更新问题以提供更多上下文
-
能不能不用NSPredicate的arrayArgument init? -
init(format predicateFormat: String, argumentArray arguments: [AnyObject]?) -
不,我收到构建错误
Value of type 'Self.IdentityType' does not conform to expected element type 'AnyObject' -
您的协议没有从 NSObjectProtocol 继承,应该可以解决这个问题。无论如何,结构和枚举可能无法与 NSPredicate 很好地混合。实际上,您可以将其转换为 AnyObject,因为它是此扩展中的 NSManagedObject。
标签: swift