قالب وردپرس درنا توس
Home / IOS Development / (Generics) How to put your property when I know it will be available? : iOSProgramming

(Generics) How to put your property when I know it will be available? : iOSProgramming



Hello everyone,

TLDR: I'm trying to put a generic property that I know will be there. Is this possible? Here's what I have right now:

  private func saveSite  (..., siteType: T, ...) {
let siteObject = NSManagedObject (context: context) like! T
siteObject.title = "Web site title here" // error states: Value of type & # 39; T & # 39; has no member title & # 39;
}

Explanation:

This is a bit short for simplification, but here is an analog example: When using Core Data I have a device called "Subject" that has a One-to-Many relationship with "GoogleSite »(Optional) using the` googleSites` property, which contains references from Google, if any. It also has a One-to-Many relationship with "BingSite" for Bing sites with "bingSites" (optional).

The site structure is exactly the same (only `title`,` url` and `topic` properties), and the only reason I do this is to satisfy the Core Data inverse relationship. It would not work with a general `Site` structure, since it could only be related to 'Subject' via a property, as far as I could tell (` googleSites` and `bingSites`), and I need them to be in separate arrays.

So long story short, I know the title feature will be there in the generics. Is it possible to make this work with the example above?

Page Questions: Is it possible to have just one `Site` unit in my model to get rid of all the extra noise? And as someone new to Core Data and Generics, is there something I clearly misunderstand here?

Thank you very much in advance!

Edit: I understand that I must include the description when initializing the managed object. Anyway, any insight into navigating in this room is greatly appreciated!


Source link