Objective-c 代码可以在 Class 上调用 swift 扩展吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27097688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:30:42  来源:igfitidea点击:

Can objective-c code call swift extension on Class?

objective-cswiftswift-extensions

提问by sprhawk

I searched some posts, I think I cannot write an extension under swift, and call it from Objective-C code, right ?

我查了一些帖子,我觉得我不能在swift下写一个扩展,并从Objective-C代码中调用它,对吧?

@objc like attributes only support methods, class, protocols ?

@objc 之类的属性只支持方法、类、协议?

回答by marius bardan

You can write a Swift extension and use it in Objective-C code. Tested with XCode 6.1.1.

您可以编写一个 Swift 扩展并在 Objective-C 代码中使用它。使用 XCode 6.1.1 测试。

All you need to do is:

您需要做的就是:

  • create your extension in Swift (no @objcannotation)

  • #import "ProjectTarget-Swift.h"in your Objective-C class (where "ProjectTarget" represents the XCode target the Swift extension is associated with)

  • call the methods from the Swift extension

  • 在 Swift 中创建您的扩展(无@objc注释)

  • #import "ProjectTarget-Swift.h"在您的 Objective-C 类中(其中“ProjectTarget”代表与 Swift 扩展关联的 XCode 目标)

  • 从 Swift 扩展中调用方法

Update:As of @Rizwan Ahmed mentioned, you need to add @objcannotation:

更新:正如@Rizwan Ahmed 所提到的,您需要添加@objc注释:

As of Swift 4.0.3, the @objc annotation is needed if you want to use the extension in Objective C class files.

从 Swift 4.0.3 开始,如果您想在 Objective C 类文件中使用扩展,则需要 @objc 注释。

回答by Andrey P.

I found out that in Swift 4.0I had to add @objcin front of my extensionkeyword in order for the Swift extension methods to become visible by an instance of the Objc class I was extending.

我发现在Swift 4.0 中,我必须@objc在我的扩展关键字前面添加,以便 Swift 扩展方法对我正在扩展的 Objc 类的实例可见。

In short:

简而言之:

File configuration setup:

文件配置设置:

CustomClass.h
CustomClass.m
CustomClassExtension.swift

In CustomClassExtension:

在自定义类扩展中:

@objc extension CustomClass
{
    func method1() 
    {
        ...
    }
}

In my AppDelegate.m:

在我的 AppDelegate.m 中:

self.customClass = [[CustomClass alloc] init];
[self.customClass method1];

回答by Foti Dim

This solution works for Swift 2.2 and Swift 3. Note that only extensions for classes (not for structs or enums) will be accessible from Objective-C.

此解决方案适用于Swift 2.2 和 Swift 3。请注意,Objective-C 只能访问类的扩展(不适用于结构或枚举)。

import UIKit

extension UIColor {

    //Custom colours
    class func otherEventColor() -> UIColor {
        return UIColor(red:0.525, green:0.49, blue:0.929, alpha:1)
    }
}

Then #import "ProductModuleName-Swift.h"in your ObjC file.

然后在 ObjC 文件中#import "ProductModuleName-Swift.h"

Swift 4

斯威夫特 4

extension UIColor {

    // As of Swift 4.0.3, the @objc annotation is needed if you want to use the extension in Objective-C files
    @objc
    class func otherEventColor() -> UIColor {
        return UIColor(red:0.525, green:0.49, blue:0.929, alpha:1)
    }
}

回答by mclaughlinj

As covered in the other answers, importing the generated Swift header works in most cases.

正如其他答案中所述,导入生成的 Swift 标头在大多数情况下都有效

An exception to this is when the category is defined on a bridged type (i.e. the extension is defined on Stringand not NSString). These categories will not automatically be bridged to their Objective-C counterparts. To get around this, you'll either need to use the Objective-C type (and cast the return value in your Swift code with as String) or define an extension for both the Swift and Objective-C types.

一个例外是当类别定义在桥接类型上时(即扩展定义在String而不是NSString)。这些类别不会自动桥接到它们的 Objective-C 对应物。要解决此问题,您需要使用 Objective-C 类型(并使用 将返回值转换为 Swift 代码as String)或为 Swift 和 Objective-C 类型定义扩展。

回答by Ranjani

Import "#import "ProductModuleName-Swift.h"header in objective-c file and add @objcinfront of your extentsionin swift file. It will working fine in swift 4.2 and swift 5

在objective-c 文件中导入 "#import"ProductModuleName-Swift.h"标头,并在swift 文件中的extentsion 前面添加@objc。它将在swift 4.2 和swift 5 中正常工作