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属性只支持方法、类、协议?
4 个解决方案
#1
49
You can write a Swift extension and use it in ObjectiveC code. Tested with XCode 6.1.1.
您可以编写一个快速扩展并在ObjectiveC代码中使用它。与XCode但是测试。
All you need to do is:
你所需要做的就是:
-
create your extension in Swift (no @objc annotation)
在Swift中创建扩展(没有@objc注释)
-
import "ProjectTarget-Swift.h" in your ObjectiveC class (where "ProjectTarget" represents the XCode target the Swift extension is associated with)
导入“ProjectTarget-Swift。在您的ObjectiveC类中的h(其中“ProjectTarget”表示与Swift扩展相关联的XCode目标)
-
call the methods from the Swift extension
调用来自Swift扩展的方法
#2
36
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 otheEventColor() -> UIColor {
return UIColor(red:0.525, green:0.49, blue:0.929, alpha:1)
}
}
Then #import "ProductModuleName-Swift.h" in your ObjC file.
然后ProductModuleName-Swift #进口”。h"在你的ObjC文件中。
#3
34
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 String
and 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.
一个例外是当类别定义为桥接类型时(即扩展定义为字符串而非NSString)。这些类别不会自动地连接到它们的Objective-C对应项。要解决这个问题,您需要使用Objective-C类型(并将Swift代码中的返回值转换为字符串),或者为Swift和Objective-C类型定义扩展。
#4
28
I found out that in Swift 4.0 I had to add @objc
in front of my extension keyword in order for the Swift extension methods to become visible by an instance of the Objc class I was extending.
我发现在Swift 4.0中,我必须在我的extension关键字前面添加@objc,以便Swift扩展方法被我正在扩展的Objc类的实例可见。
In short:
简而言之:
File configuration setup:
文件配置设置:
CustomClass.h
CustomClass.m
CustomClassExtension.swift
In CustomClassExtension:
在CustomClassExtension:
@objc extension CustomClass
{
func method1()
{
...
}
}
In my AppDelegate.m:
在我AppDelegate.m:
self.customClass = [[CustomClass alloc] init];
[self.customClass method1];
#1
49
You can write a Swift extension and use it in ObjectiveC code. Tested with XCode 6.1.1.
您可以编写一个快速扩展并在ObjectiveC代码中使用它。与XCode但是测试。
All you need to do is:
你所需要做的就是:
-
create your extension in Swift (no @objc annotation)
在Swift中创建扩展(没有@objc注释)
-
import "ProjectTarget-Swift.h" in your ObjectiveC class (where "ProjectTarget" represents the XCode target the Swift extension is associated with)
导入“ProjectTarget-Swift。在您的ObjectiveC类中的h(其中“ProjectTarget”表示与Swift扩展相关联的XCode目标)
-
call the methods from the Swift extension
调用来自Swift扩展的方法
#2
36
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 otheEventColor() -> UIColor {
return UIColor(red:0.525, green:0.49, blue:0.929, alpha:1)
}
}
Then #import "ProductModuleName-Swift.h" in your ObjC file.
然后ProductModuleName-Swift #进口”。h"在你的ObjC文件中。
#3
34
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 String
and 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.
一个例外是当类别定义为桥接类型时(即扩展定义为字符串而非NSString)。这些类别不会自动地连接到它们的Objective-C对应项。要解决这个问题,您需要使用Objective-C类型(并将Swift代码中的返回值转换为字符串),或者为Swift和Objective-C类型定义扩展。
#4
28
I found out that in Swift 4.0 I had to add @objc
in front of my extension keyword in order for the Swift extension methods to become visible by an instance of the Objc class I was extending.
我发现在Swift 4.0中,我必须在我的extension关键字前面添加@objc,以便Swift扩展方法被我正在扩展的Objc类的实例可见。
In short:
简而言之:
File configuration setup:
文件配置设置:
CustomClass.h
CustomClass.m
CustomClassExtension.swift
In CustomClassExtension:
在CustomClassExtension:
@objc extension CustomClass
{
func method1()
{
...
}
}
In my AppDelegate.m:
在我AppDelegate.m:
self.customClass = [[CustomClass alloc] init];
[self.customClass method1];