我将Xcode 8 CoreData与自动生成的基类一起使用。
当我尝试
let fetchRequest: NSFetchRequest<Event> = Event.fetchRequest()
fetchRequest变量正确获取NSFetchRequest<Event>
当我尝试
let fetchRequest = Event.fetchRequest()
Xcode告诉fetchRequest有未定义的类型,据我所知,Swift必须通过赋值自动确定类型
这是由Xcode生成的自动生成的类扩展
extension Event {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Event> {
return NSFetchRequest<Event>(entityName: "Event");
}
@NSManaged public var ffff: String?
@NSManaged public var timestamp: NSDate?
}
作为示例,此代码正常工作(逻辑是相同的)
struct MyStruct<T> {
let myVar: T
}
class MyClass {
}
extension MyClass {
class func test() -> MyStruct<Int> {
return MyStruct<Int>(myVar: 5)
}
}
let b = MyClass.test()
let b
的{{1}}
答案 0 :(得分:0)
CoreDate自动生成
@objc(Event)
public class Event: NSManagedObject {
}
extension Event {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Event> {
return NSFetchRequest<Event>(entityName: "Event");
}
@NSManaged public var timestamp: NSDate?
}
NSManagedObject
协议在Objective-C中,它有类方法
+ (NSFetchRequest*)fetchRequest
当我尝试使用let fetchRequest = Event.fetchRequest()
时,它认为我称之为Objective-C + (NSFetchRequest*)fetchRequest
而且它没有泛型
为什么它不使用Event类扩展中的重写@nonobjc public class func fetchRequest() -> NSFetchRequest<Event>
?