1

I'm currently in Swift 3.1 and the following code does not compile.

protocol HeaderDisplayable { func setTitle(_ string: String) } class MyView: UILabel, HeaderDisplayable { func setTitle(_ string: String) { self.text = string } } func foo<T: UILabel>(view:T) where T: HeaderDisplayable{ view.setTitle("HEY") } foo(MyView()) 

The error message is saying

Generic parameter 'T' could not be inferred

1 Answer 1

3

The error is a bit misleading but you are missing the argument label. You need to do this:

foo(view: MyView()) 

or if you want to remove the need for it you could change the method definition to this:

func foo<T: UILabel>(_ view:T) where T: HeaderDisplayable{ view.setTitle("HEY") } 

Either version will work fine.

Sign up to request clarification or add additional context in comments.

1 Comment

Ha that was stupid of me! The error message did not help at all

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.