4

I updated Xcode to 10.2 today and I got the following errors:

Method cannot be marked @objc because the type of the parameter 2 cannot be represented in Objective-C

Function types cannot be represented in Objective-C unless their parameters and returns can be I don't understand why

It was perfectly fine in 10.1. This is an example that I have been using for years without any issues. How can I make this code to compile without errors?

@objc public func myFunction(inputString: String, handler:@escaping ((success: Bool, outPut: NSArray)) -> Void) {
    // do stuff
}
Tibidabo
  • 21,461
  • 5
  • 90
  • 86
  • 1
    Is that a member function of a *class?* – My Xcode 10.2 complains *“Cannot create a single-element tuple with an element label”* with the Fix-it *“Replace 'outPut: ' with ''”* – Martin R Mar 27 '19 at 02:43
  • @MartinR I removed the element label and that solved the issue see the answer from matt – Tibidabo Mar 27 '19 at 02:59
  • I know that is does. But if I cannot reproduce the *exact* error message as reported then I wonder if the exact code was posted :) – Martin R Mar 27 '19 at 03:09
  • @MartinR Sorry, you are right. I simplified it... It should have more than one element in the tuple. it should be: func myFunction(inputString: String, handler:@escaping ((success: Bool, outPut: NSArray)) -> Void) { // do stuff } – Tibidabo Mar 27 '19 at 06:18
  • I am glad that your problem is solved. But please update your question with actual code which produces that error message – that makes it more likely that other people with the same problem find this Q&A and the solution. See also https://stackoverflow.com/help/on-topic: *“Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers.”* – Martin R Mar 27 '19 at 07:39
  • @MartinR I updated the question with the correct code. Thanks – Tibidabo Mar 27 '19 at 12:50
  • 2
    Now it gets interesting: Your closure argument has an extra pair of parentheses, apparently Swift 4 ignores that but Swift 5 does not. If you remove that extra pair: `handler:@escaping (success: Bool, outPut: NSArray) -> Void` then you'll get a clear error message *“Function types cannot have argument labels; use '_' before 'outPut'”* already in Swift 4/Xcode 10.1. – So what changed is not the forbidden external parameter names (they are already forbidden in Swift 4), but how the compiler treats the extra pair of parentheses. – Martin R Mar 27 '19 at 12:59

1 Answers1

2

Delete the phrase outPut:. It was always illegal; Swift 5 just tightens up at last.

So:

@objc public func myFunction(inputString: String, handler:@escaping (NSArray) -> Void) {
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • You should not be using NSArray either but that’s a different issue. – matt Mar 27 '19 at 02:43
  • Thank you Matt. It did the trick. Is there any way to assign phrases to the variables that is not illegal? – Tibidabo Mar 27 '19 at 02:57
  • 2
    @Tibidabo: You can define the parameter as `handler: @escaping (_ output: NSArray) -> Void`. That also allows to *document* the parameter, see https://stackoverflow.com/a/38669796/1187415. – Martin R Mar 27 '19 at 03:01