2

I have a class that conforms to an Objective-C protocol and has a function with the same name as one of it's parameter types.

class MessageDataController: NSObject, MCOHTMLRendererDelegate {
    @objc func MCOAbstractMessage(msg: MCOAbstractMessage!, canPreviewPart part: MCOAbstractPart!) -> Bool {
        return false
    }
}

This causes Xcode to give the error

"Use of undeclared type 'MCOAbstractMessage'"

for using MCOAbstractMessage as both the function name and a parameter type. It doesn't give an error if I change the function name to abstractMessage or similar. I think the issue is related to this question and/or this issue but am unsure how to resolve. My project's header file is correctly configured to use MailCore2.

Tried changing the declaration to:

@objc(MCOAbstractMessage:canPreviewPart:) func abstractMessage(msg: MCOAbstractMessage!, canPreviewPart part: MCOAbstractPart!) -> Bool

which gives the error

"~/src/project/MessageDataController.swift:11:52: Objective-C method 'MCOAbstractMessage:canPreviewPart:' provided by method 'abstractMessage(:canPreviewPart:)' conflicts with optional requirement method 'MCOAbstractMessage(:canPreviewPart:)' in protocol 'MCOHTMLRendererDelegate'"

Community
  • 1
  • 1
Comrade_Question
  • 305
  • 1
  • 2
  • 10
  • 2
    You really shouldn't use uppercase for method names... – Kametrixom Nov 05 '15 at 20:51
  • 1
    Not my choice. The protocol defines this particular method as `- (BOOL) MCOAbstractMessage:(MCOAbstractMessage *)msg canPreviewPart:(MCOAbstractPart *)part;`. See [here](https://github.com/MailCore/mailcore2/blob/master/src/objc/abstract/MCOHTMLRendererDelegate.h) for the protocol I'm trying to implement. – Comrade_Question Nov 05 '15 at 20:58
  • Your first method is implemented right. But the Swift code cannot understand `@class MCOAbstractMessage` defined in Objective C. – t4nhpt Nov 06 '15 at 01:56
  • Would there be a way to achieve it without changing the ObjC code? For example, is there a way to redefine the Swift<->ObjC name mapping? – Dinh Viêt Hoà Nov 11 '15 at 21:09
  • 1
    Could you take a look at the following? https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html and look for NS_SWIFT_NAME. You might be interested in applying a patch to MailCore2 and send a pull request to the project. Thanks! – Dinh Viêt Hoà Nov 12 '15 at 18:01
  • @DinhViêtHoà Will do. Thanks! – Comrade_Question Nov 13 '15 at 18:50
  • @DinhViêtHoà Unfortunately, the NS_SWIFT_NAME macro only applies to class factory methods. – Comrade_Question Nov 17 '15 at 16:40
  • @Kametrixom: The casing is correct for ObjC. Prefixes are generally capitalized. – jscs Nov 17 '15 at 19:26

1 Answers1

0

This may be solved by using the fully-qualified type name in the parameter list. I'm not familiar with the library you are using, but the suggestion below assumes that the type MCOAbstractMessage is declared in a module called MCO. Prepend MCO. to the type name.

class MessageDataController: NSObject, MCOHTMLRendererDelegate {
    @objc func MCOAbstractMessage(msg: MCO.MCOAbstractMessage!, canPreviewPart part: MCOAbstractPart!) -> Bool {
        return false
    }
}

I tested this by adding a method called Array to one of my classes. Sure enough it threw compiler errors everywhere else that I had used Array as a type. I prefixed all of those as Swift.Array and all was well.

If you want a shorter version, use a typealias,

Chris Trahey
  • 18,202
  • 1
  • 42
  • 55
  • Thanks, but it didn't work. Where would I find the module declaration, if any? [Here](https://github.com/MailCore/mailcore2/tree/master/src/objc) is the library I'm working with. – Comrade_Question Nov 17 '15 at 19:44
  • Check out this reference: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html – Chris Trahey Nov 17 '15 at 20:10
  • As a followup, typealias doesn't work because I still get the `Use of undeclared type` error mentioned in my post, as well as a `Type alias circularly references itself` error. – Comrade_Question Nov 17 '15 at 21:23