What are rules of parameter name shortening in ObjC protocols?

Viewed 78

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?
1 Answers

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

Related