Subclass.fetchRequest() Swift 3.0, extension not really helping 100%?

后端 未结 3 410
长发绾君心
长发绾君心 2021-01-03 18:05

according to the new Core Data changes, Xcode generates this subclass for me:

extension Person {

    @nonobjc public class func fetchRequest() -> NSFetch         


        
相关标签:
3条回答
  • 2021-01-03 18:37

    Because fetchRequest() is generic for all entity,you can see same method in all your generated extensions , you have to make it specific .

    Below line is specific to YourEntity class fetch request method

    let fetchRequest: NSFetchRequest<**YourEntity**> = **YourEntity**.fetchRequest()
    
    0 讨论(0)
  • 2021-01-03 18:51

    The reason this is happening is there are two methods named fetchRequest in your Person class:

    • first is in your model subclass (Person) with return type NSFetchRequest<Person>
    • second is in NSManagedObject with return type NSFetchRequest<NSFetchRequestResult>

    That's actually why it is ambiguous, compiler does not know which out of 2 you refer to. If you rename func name in your Person+CoreDataProperties to from fetchRequest to personFetchRequest and call by that name - it would compile just like that.

    Even better - just add another func to your Person+CoreDataClass with different name, which would return Person.fetchRequest(), and you won't need to cast when calling by that name.

    0 讨论(0)
  • 2021-01-03 18:53

    The reason is the generic type. NSFetchRequest has been turned into a generic to avoid a lot of type casting.

    If the function doesn't take any parameter so the concrete type (Person) cannot be passed directly and the return type is also a generic it must be annotated explicitly.

    0 讨论(0)
提交回复
热议问题