[swift-users] @objc and private methods

Tod Cunningham tcunningham at vectorform.com
Thu Jul 21 12:03:03 CDT 2016


If Test and Test2 are in separate files, swift doesn’t allow you to do the override as it “can’t see” the definition of the private method in Test.


From: Zhao Xin <owenzx at gmail.com>
Date: Thursday, July 21, 2016 at 11:06 AM
To: Tod Cunningham <tcunningham at vectorform.com>
Cc: "swift-users at swift.org" <swift-users at swift.org>
Subject: Re: [swift-users] @objc and private methods

I think @objc makes the function an Objective-C function, so the private is no longer making the function private in Swift way, but in Objective-C way. In C++,  sub-class can call super-class's private method. What you have to do is to override the function.


class Test {

    @objc private func somefunc() {

        print( "hello 1" )

    }

}



class Test2: Test {

    @objc override private func somefunc() {

        print( "hello 2" )

    }

}



Zhaoxin





On Thu, Jul 21, 2016 at 10:25 PM, Tod Cunningham via swift-users <swift-users at swift.org<mailto:swift-users at swift.org>> wrote:
I wanted to get thoughts on the use of @objc and private methods.  Should this usage be avoided or leveraged?

I have seen some code where things like UIButton and Gesture recognizer handlers are defined as being private.  I would have thought this would result in a compiler error given objective-c doesn’t support private accessors.  However, it compiles and works as expected (kind of).

One plus for defining the handlers as private is they aren’t visible to swift outside of the file they are defined in.  The problem is you can run into some issues with inheritance if you define the same handler in a derived class.  Take the following simple example:

class Test {
    @objc private func somefunc() {
        print( "hello 1" )
    }
}

-- in another file –

class Test2: Test {
    @objc private func somefunc() {
        print( "hello 2" )
    }
}

This will result in the following compiler error:

Method 'somefunc()' with Objective-C selector 'somefunc' conflicts with method 'somefunc()' from superclass 'Test' with the same Objective-C selector

That error makes sense in an Objective-C context as Test2 has two different methods defined with the same objective-c selector (somefunc).  Swift can tell them apart as they are private to each class, but Obj-C can’t tell them apart as they have the same selector thus the error.

One workaround would be to give the method a different Objective-C names such as @objc(test2_somefunc); however, that could really lead to some unexpected results depending on what selector is used to call somefunc.  Test2 would need to “register” a different selector for somefunc then Test does for whatever is calling it on the Objective-C side (yuck!).

This can of course be solved by not making the method private which allows Test2 to explicitly override somefunc.

class Test {
   @objc func somefunc() {
       print( "hello 1" )
   }
}

-- in another file –

class Test2: Test {
   @objc override func somefunc() {
       print( "hello 2" )
   }
}

I would say that when declaring @objc methods they shouldn’t be private and should have the same (public/internal) access method as their containing class.  What’s your thoughts?  Why does swift allow a method to be declared as both @objc and private?

Thanks,



_______________________________________________
swift-users mailing list
swift-users at swift.org<mailto:swift-users at swift.org>
https://lists.swift.org/mailman/listinfo/swift-users



More information about the swift-users mailing list