3

I am trying to generate sample data for an @IBDesignable control, so when building for IB I'm fooling the control into being its own datasource. The upshot is I'm adding some methods to a protocol only for use by IB, and as a good citizen I wish to remove these for a regular (non-IB build).

I've distilled my problem down to the following code fragment. My protocol looks like this:-

protocol TestProtocol {
#if TARGET_INTERFACE_BUILDER
    func myControl(control:AnyObject, colorForIndex index:UInt) -> UIColor?
    func myControl(control:AnyObject, textForIndex index:UInt) -> String?
#endif
}

This fails to compile, with a message that implies an method overloading error: "error: declaration conflicts with previous value". The error message is on the second function declaration, and refers to the first function as the previous declaration it's clashing with.

But these are not overloads, they have different signatures because of the named parameters. And this is such a standard delegate naming convention across Cocoa that I was resistant to renaming my methods without understanding why.

Removing the #if TARGET_INTERFACE_BUILDER fixes the problem, so it is no longer a pressing issue for me, but I am completely stumped as to why adding this conditional compilation would produce such a bizarre error?

Echelon
  • 7,306
  • 1
  • 36
  • 34

1 Answers1

0

I am not sure why it happens, but happen to find a reasonable workaround.
Just separate the two declarations as shown below:

protocol TestProtocol {

#if TARGET_INTERFACE_BUILDER
    func myControl(control:AnyObject, colorForIndex index:UInt) -> UIColor?
#endif

#if TARGET_INTERFACE_BUILDER
    func myControl(control:AnyObject, textForIndex index:UInt) -> String?
#endif

}
K.K
  • 2,647
  • 1
  • 26
  • 32