0

I use Xcode 8 CoreData with auto generated Base classes.

When I try let fetchRequest: NSFetchRequest<Event> = Event.fetchRequest()

fetchRequest variable correctly gets type of NSFetchRequest<Event>

When I try let fetchRequest = Event.fetchRequest()

Xcode tells that fetchRequest has undefined type, as I understand Swift must determine type automatically by making assignment

Here is the auto generated class extension generated by 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?

}

As an example this code works correctly (logic is the same)

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 has a correct type of MyStruct<Int>

Shadowfax
  • 1,567
  • 2
  • 13
  • 16
  • Looks like a duplicate of http://stackoverflow.com/questions/37810967/how-to-apply-the-type-to-a-nsfetchrequest-instance to me. – Martin R Oct 27 '16 at 11:56
  • The question is why using `let fetchRequest = Event.fetchRequest()` does not determine the type of fetchRequest but using `let fetchRequest: NSFetchRequest = Event.fetchRequest()` works – Shadowfax Oct 27 '16 at 11:59

1 Answers1

0

CoreDate automatically generated

@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 protocol is in Objective-C and it has class method

+ (NSFetchRequest*)fetchRequest 

When i try to use let fetchRequest = Event.fetchRequest() it thinks that i call Objective-C + (NSFetchRequest*)fetchRequest and it has no generics

Why doesn't it use an overridden @nonobjc public class func fetchRequest() -> NSFetchRequest<Event> from an Event class extension?

Shadowfax
  • 1,567
  • 2
  • 13
  • 16