简体   繁体   English

仅将 Swift 编译器方法称为 swift

[英]Tell Swift compiler method as swift only

I have a swift class defined like this:我有一个 swift class 定义如下:

@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: objective-c 里面有很多方法,所以我直接用@objcMembers作为class 前缀,然后swift 编译器开始抱怨这个:

Method 'concat' with Objective-C selector 'concat:' conflicts with previous declaration with the same Objective-C selector方法 'concat' 与 Objective-C 选择器 'concat:' 与先前使用相同 Objective-C 选择器的声明冲突

They are exactly have the same function signature, but the latter one is only available for swift even if exposed.它们具有完全相同的 function 签名,但后者即使暴露也仅适用于 swift。 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.现在我正在寻找一些编译标志来将后一个concat方法标记为swift 只是为了告诉编译器忽略冲突错误。

@objc and @objcMembers do that explicitly, so how to do it reverse? @objc@objcMembers明确地做到了这一点,那么如何反过来呢?

https://docs.swift.org/swift-book/ReferenceManual/Attributes.html#nonobjc 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. “nonobjc 将此属性应用于方法、属性、下标或初始化程序声明以抑制隐式 objc 属性。 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. nonobjc 属性告诉编译器使声明在 Objective-C 代码中不可用,即使它可以在 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.将此属性应用于扩展与将其应用于该扩展中未显式标记为 objc 属性的每个成员具有相同的效果。

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.您可以使用 nonobjc 属性来解决标记为 objc 属性的 class 中桥接方法的循环性,并允许在标记为 objc 属性的 class 中重载方法和初始化程序。

A method marked with the nonobjc attribute can't override a method marked with the objc attribute.用 nonobjc 属性标记的方法不能覆盖用 objc 属性标记的方法。 However, a method marked with the objc attribute can override a method marked with the nonobjc attribute.但是,标有 objc 属性的方法可以覆盖标有 nonobjc 属性的方法。 Similarly, a method marked with the nonobjc attribute can't satisfy a protocol requirement for a method marked with the objc attribute.”同样,标有 nonobjc 属性的方法不能满足标有 objc 属性的方法的协议要求。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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