2

I have following problem: I have ObjC protocol with swift implementation:

// ObjC protocol

@protocol PhonebookSomeEventCallback

- (void)onEvent:(nonnull NSUUID *)outUserId
   someNumValue:(int32_t)someNumValue
   lotOfPersons:(nonnull NSArray<PhonebookPerson *> *)lotOfPersons;

@end
//=====================================
// Swift impl
class PhonebookSomeEventCallbackImpl: PhonebookSomeEventCallback
{
    public func onEvent(_ outUserId: UUID,someNumValue: Int32,lotOfPersons: [PhonebookPerson])
    {
    }
}

And it fails to build with following error:

 error: instance method 'onEvent(_:someNumValue:lotOfPersons:)' has different argument labels from those required by protocol 'PhonebookSomeEventCallback' ('onEvent(_:someNumValue:lotOf:)')
    public func onEvent(_ outUserId: UUID,someNumValue: Int32,lotOfPersons: [PhonebookPerson])
                ^
                                                              lotOf 
Phonebook.PhonebookSomeEventCallback:3:10: note: requirement 'onEvent(_:someNumValue:lotOf:)' declared here
    func onEvent(_ outUserId: UUID, someNumValue: Int32, lotOf lotOfPersons: [PhonebookPerson])
         ^
/Users/g.a.igumnov/work/mobile/phonebook/controller/mobile-app/x86_64/subprojects/controller/service-sbis-phonebook/phonebook/djinni/swift/phonebook/PhonebookSomeEventEvent.swift:32:17: error: 'onEvent(_:someNumValue:lotOfPersons:)' has been renamed to 'onEvent(_:someNumValue:lotOf:)'
    public func onEvent(_ outUserId: UUID,someNumValue: Int32,lotOfPersons: [PhonebookPerson])
                ^
                                                              lotOf 
Phonebook.PhonebookSomeEventCallback:5:10: note: 'onEvent(_:someNumValue:lotOfPersons:)' was obsoleted in Swift 3
    func onEvent(_ outUserId: UUID, someNumValue: Int32, lotOfPersons: [PhonebookPerson])

Experimentally I found following principles If arguments name ends with one of keywords ('of', 'with', 'by', 'from', 'on' - those I've found) followed by type name (or part of it, I have class PhonebookPerson, but just Person works) in plural for arrays and singular for non-arrays, type name is removed from argument name.

I have two questions

  1. Is there any documentation on this black magic?
  2. What is the proper way to avoid it except for not writing such names?
Crazy Sage
  • 414
  • 2
  • 14
  • Maybe there https://stackoverflow.com/questions/40157131/converting-to-swift-3-renamed-my-own-objective-c-method you might have a few links and reading With `NS_SWIFT_NAME` https://developer.apple.com/documentation/swift/objective-c_and_c_code_customization/renaming_objective-c_apis_for_swift you might force a renaming on the method I think. – Larme Nov 12 '21 at 11:52
  • Main problem is that this code is automatically generated c++<->swift bridge, based on pseudocode. And I need to understand rules of how that renaming work, to be able to generate proper code (or at least show users proper error, why they can't name theyr arguments like that). – Crazy Sage Nov 12 '21 at 12:01
  • On the linked question, there is a link "SE-0005 Better Translation of Objective-C APIs Into Swift" that should add details... – Larme Nov 12 '21 at 12:24

1 Answers1

0

I put your protocol in my test project and used the short cut to let xcode autgenerate the protocol functions this is what it made

class PhonebookSomeEventCallbackImpl: PhonebookSomeEventCallback
{
    // I replaced the PhonebookPerson with UIView
    //
    // Also, ou can use auto-generation by complying with the protocol, compiling
    // and then pressing cmd+option+ctrl + F to have it generate the function
    func onEvent(_ outUserId: UUID, someNumValue: Int32, lotOfPersons: [UIView]) {
        <#code#>
    }

}

Excluding my replacement of the UIView it seems like you're doing the right stuff.

It seems like you're doing everything right, and there isn't black magic for how it converts obj-c functions to swift. The first item won't have a parameter name and all the other parameter names should be 1-1.

P.S. You can also auto-generate the functions by using the shortcut after the compile error pops up

enter image description here

enter image description here

Yogurt
  • 2,913
  • 2
  • 32
  • 63
  • 1
    I can't auto-generate functions using shortcut, because this code is auto-generated by python scripts and I need to understand rules to process them in my scripts correctly. Though it is strange, that in my case swift compiler requires third parameter to be called lotOf, instead of lotOfPerson and states, that lotOfPerson is obsolete since swift3 – Crazy Sage Nov 21 '21 at 05:32