简体   繁体   中英

Tell Swift compiler method as swift only

I have a swift class defined like this:

@objcMembers
public class MyURL: NSObject {

    func concat(_ components: String...) -> MyURL {
        concat(components)
    }

    /// Concat the specified components one by one.
    func concat(_ components: [String]) -> MyURL {
        components.forEach { let _ = value?.appendingPathComponent($0) }
        return self
    }

    // All the other methods which are available for objc.
}

There are many methods inside which are available for objective-c, so I use the @objcMembers for class prefix directly, then swift compiler starts to complaint this:

Method 'concat' with Objective-C selector 'concat:' conflicts with previous declaration with the same Objective-C selector

They are exactly have the same function signature, but the latter one is only available for swift even if exposed. Now I'm looking for some compile flags to mark the latter concat method as swift only to tell the compiler to ignore the conflict error.

@objc and @objcMembers do that explicitly, so how to do it reverse?

https://docs.swift.org/swift-book/ReferenceManual/Attributes.html#nonobjc

“nonobjc Apply this attribute to a method, property, subscript, or initializer declaration to suppress an implicit objc attribute. The nonobjc attribute tells the compiler to make the declaration unavailable in Objective-C code, even though it's possible to represent it in Objective-C.

Applying this attribute to an extension has the same effect as applying it to every member of that extension that isn't explicitly marked with the objc attribute.

You use the nonobjc attribute to resolve circularity for bridging methods in a class marked with the objc attribute, and to allow overloading of methods and initializers in a class marked with the objc attribute.

A method marked with the nonobjc attribute can't override a method marked with the objc attribute. However, a method marked with the objc attribute can override a method marked with the nonobjc attribute. Similarly, a method marked with the nonobjc attribute can't satisfy a protocol requirement for a method marked with the objc attribute.”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM