0

I know the title of the question is not correct for the question. But I could not come up with better title, Ill modify if somebody suggests a better title.

Here is the question:

I have a protocol,

import Foundation

protocol MyFilter {
    var currentlySelected : Bool { get set }
    func configure<T> (filterInfo : T) where T: MyGenericDataModel
}

This asks all the confirming classes to declare a variable named currentlySelected and also expects to write a method that takes a single parameter that confirms to MyGenericDataModel protocol.

MyGenericDataModel looks like,

import Foundation

protocol MyGenericDataModel {
    init(data : JSON)
}

There are multiple classes that confirms to MyGenericDataModel. Like,

struct MyDataModel1 : MyGenericDataModel {
    var id : String? = nil
    //other properties

    init(data : JSON) {
    }
}

struct MyDataModel2 : MyGenericDataModel {
    var id : String? = nil
    //other properties

    init(data : JSON) {
    }
}

and so on.

Now I get the array of data which can be any of the custom data model. So I declared a array of objects which confirms to MyGenericDataModel protocol.

var passedDataSource: Array< MyGenericDataModel>? = nil

Now I need to call the method of MyFilter,

let filterInfo = passedDataSource![indexPath.row]
abcd.configure(filterInfo: filterInfoToPass)

Where abcd obviously confirms to MyFilter thats why I can call configure on it.

Issue

Xcode gives compilation error saying,

Can not invoke 'configure' with an argument list of type '(filterInfo: MyGenericDataModel)

EDIT:

As per luk2302's comment, I am posting the implementation details of class of abcd :)

class MyCellClass: UICollectionViewCell, MyFilter{
    var currentlySelected : Bool = false
    func configure<T>(filterInfo: T) where T : MyGenericDataModel {
       //use filter info
       //perform some UI operations to update cell UI
    }
}

Please help. Thanks in advance.

Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78
  • If you would show what `abcd` you would present a *fully* working example. – luk2302 Jan 18 '17 at 09:46
  • 1
    Is there *any* reason for the use of generics at all? Why not use `func configure (filterInfo : MyGenericDataModel)` instead? – luk2302 Jan 18 '17 at 09:49
  • @luk2302 : Sure sir! Lemme post the implementation of abcd class and as for your second question why Generics at all, I was trying to learn the concept hence I decided to declare it as Generics :) – Sandeep Bhandari Jan 18 '17 at 09:52
  • Try changing the signature of configure to `func configure (filterInfo : T)` – Mark Brownsword Jan 18 '17 at 10:00
  • @mark-brownsword : Tried :( no benefit still same error appears :( – Sandeep Bhandari Jan 18 '17 at 10:02
  • What does Xcode think the input parameter type should be, is there a hint for type? – Mark Brownsword Jan 18 '17 at 10:29
  • No, Xcode does not show any further details. It has a problem if I pass the parameter of type MyGenericDataModel on the other hand if I pass parameter of type MyDataModel1 which confirms to MyGenericDataModel it accepts without any complains – Sandeep Bhandari Jan 18 '17 at 10:35
  • Is that `filterInfo` variable name wrong? It should be `let filterInfoToPass = passedDataSource![indexPath.row]` – Mark Brownsword Jan 18 '17 at 10:39
  • Nope :) Sorry that was copy paste mistake, I could not post the original source code hence tried renaming variables and removed all un related logics in question. While copy pasting I did mistake :) – Sandeep Bhandari Jan 18 '17 at 10:41
  • 1
    Because [protocols don't conform to themselves](http://stackoverflow.com/questions/33112559/protocol-doesnt-conform-to-itself), you cannot use `MyGenericDataModel` as a type that conforms to `MyGenericDataModel`. – Hamish Jan 18 '17 at 11:12
  • @hamish : Bingo :) Perfect answer :) Up voted both question and answer :) Thank you for the link :) – Sandeep Bhandari Jan 18 '17 at 20:03

0 Answers0