Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

extension Person {      @nonobjc public class func fetchRequest() -> NSFetchRequest<Person> {         return NSFetchRequest<Person>(entityName: "Person");     }      @NSManaged public var name: String?  } 

then in code I was expecting to have this one line working:

let fr = Person.fetchRequest() 

This line of code above however gives me an error:

"Ambiguous use of 'fetchRequest' 

This fixes the issue:

let fr: NSFetchRequest<Person> = Person.fetchRequest() 

So then, I am confused what is the:

NSFetchRequest<Person> 

in the:

@nonobjc public class func fetchRequest() -> NSFetchRequest<Person> { 

return type doing at all?

Shouldn't the return type allow me not to have to specify the:

NSFetchRequest<Person> 

in the let definition??

let fr: NSFetchRequest<Person> = Person.fetchRequest() 

Can anybody help me understand why it is needed even though it's part of the return type already?

like image 521
zumzum Avatar asked Sep 14 '16 16:09

zumzum


1 Answers

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.

like image 91
Dannie P Avatar answered Oct 09 '22 20:10

Dannie P