Re: Generics Problem
Re: Generics Problem
- Subject: Re: Generics Problem
- From: Jonathan Hull <email@hidden>
- Date: Sat, 19 Sep 2015 18:01:30 -0700
If you only need U to be a String or a Double, have you tried using a protocol instead (and then having both String & Double adhere to it). It doesn’t explain the compiler error, but it might work.
I would also recommend using as? instead of as! here.
Thanks,
Jon
> On Sep 19, 2015, at 10:54 AM, Michael de Haan <email@hidden> wrote:
>
> I am calling this function,
>
>
> func createMOforEntityName<T, U >(entityName:String, context:NSManagedObjectContext, key:String, keyAttribute: U) -> (Bool, T) {
>
> guard let mo:T = self.moExistsWith(entityName, key:key, keyAttribute: keyAttribute) else {
>
> let mo:T = NSEntityDescription.insertNewObjectForEntityForName(entityName, inManagedObjectContext: context) as! T
>
> return (false, mo)
>
> }
>
> return (true, mo)
> }
>
>
> with this call:
>
> let (already_Exists, aNewMO:ESLSizeMO) = createMOforEntityName("ESLSizeEntity", context: self.managedObjectContext, key:"sizeName", keyAttribute: sizeName)
>
>
>
> In this case, sizeName is a String, but key attribute could also be a Double. The Error is on the calling line:
>
> Cannot invoke 'createMOforEntityName' with an argument list of type '(String, context: NSManagedObjectContext, key: String, keyAttribute: String)'
>
> The hint says: 'Expected an argument list of type '(String, context: NSManagedObjectContext, key: String, keyAttribute: U)’
>
> My question is how to call “createMOforEntityName” when ‘U’ could either be a String, or a Double.
>
> Thanks in advance.
>
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden