如何使用通知。从swift 3扩展到Objective-C

时间:2022-09-07 07:45:23

I created an extension for Notification.Name as below :

我为通知创建了一个扩展。名称如下:

public extension Notification.Name {
      public static let blahblahblah = Notification.Name(rawValue: "blahblahblah")

}

}

Now I want to use this extension in Objective-C, but it's not accessible even if its public.

现在我想在Objective-C中使用这个扩展,但是即使它是公共的,它也是不可访问的。

Can you tell me how to access and use this swift extension in Objective-C and Swift both?

你能告诉我如何在Objective-C和swift中使用这个快速扩展吗?

previously I was using constant values in Objective-C, but now for upgrading my code, I wanna use this extension.

以前我在Objective-C中使用常量值,但是现在升级我的代码,我想使用这个扩展。

3 个解决方案

#1


6  

Notification.Name doesn't exist in Objective-C. And the Objective-C type NotificationName is really just an NSString. To use Swift stuff in Objective-C, the class must be available in both, and can't be a Swift struct (like Notification or String, say).

通知。名字在Objective-C中不存在。Objective-C类型NotificationName其实就是NSString。要在Objective-C中使用Swift代码,类必须在这两种语言中都可用,并且不能是Swift结构体(比如通知或字符串)。

To do what you want, then, you need to have two extensions:

为了做你想做的事,你需要有两个扩展:

  • one for the Swift Notification.Name, as you have; and,
  • 一个用于Swift通知。名字,你有;而且,
  • one for an Objective-C object (NSString, say, or perhaps NSNotification if you prefer).
  • 一个对象是Objective-C对象(比如NSString,或者NSNotification,如果你喜欢的话)。

1) Add an Objective-C-compatible object extension to your Swift file:

1)在Swift文件中添加objective -c兼容的对象扩展:

public extension NSNotification {
    public static let blahblahblah: NSString = "blahblahblah"
}

Note: in Swift 4, properties must be computed for Objective-C compatibility. That would look like:

注意:在Swift 4中,必须为Objective-C兼容性计算属性。这样子:

public extension NSNotification {
    public static var blahblahblah: NSString {
        return "blahblahblah"
    }
}

Note the var in the computed property: computed properties can't be immutable, so can't use let.

注意computed属性中的var: computed properties不能是不可变的,所以不能使用let。

2) In the Objective-C file, import Xcode's generated Swift header file (below any other imports):

2)在Objective-C文件中,导入Xcode生成的Swift报头文件(在任何其他导入下):

#import "YourProjectName-Swift.h"

Note: replace YourProjectName with the actual name of your project. So, if your project is named "CoolGameApp", the Swift header would be "CoolGameApp-Swift.h". If your project name has spaces, like "Cool Game App", replace them with dashes: "Cool-Game-App-Swift.h"

注意:用项目的实际名称替换您的projectname。因此,如果你的项目被命名为“CoolGameApp”,那么Swift的标题将是“CoolGameApp-Swift.h”。如果你的项目名称有空格,如“Cool Game App”,用破折号:“Cool-Game-App- swift .h”代替。

3) Rebuild the project.

3)重建项目。

Now, you should be able to access the extension in Objective-C:

现在,你应该能够访问Objective-C中的扩展:

[[NSNotificationCenter defaultCenter] postNotificationName:NSNotification.blahblahblah object:self];

#2


5  

My extension in swift file

我在swift文件中的扩展名

extension Notification.Name {
    static let purchaseDidFinish = Notification.Name("purchaseDidFinish")
}

extension NSNotification {
    public static let purchaseDidFinish = Notification.Name.purchaseDidFinish
}

// OBJECTIVE-C

#import YourProjectName-Swift.h

[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(purchaseDidFinish) name:NSNotification.purchaseDidFinish object:nil];

// SWIFT
NotificationCenter.default.addObserver(self, selector: #selector(purchaseDidFinish), name: .purchaseDidFinish, object: nil)

@objc func purchaseDidFinish(notification: Notification) {
    print("purchaseDidFinish")
}

@leanne's answer was super helpful

@leanne的回答非常有帮助

#3


3  

In addition to the answers here, I had to add @objc to my NSNotification extension before my Obj-C code could see it (Swift 4).

除了这里的答案之外,我还必须将@objc添加到NSNotification扩展中,然后我的object - c代码才能看到它(Swift 4)。

#1


6  

Notification.Name doesn't exist in Objective-C. And the Objective-C type NotificationName is really just an NSString. To use Swift stuff in Objective-C, the class must be available in both, and can't be a Swift struct (like Notification or String, say).

通知。名字在Objective-C中不存在。Objective-C类型NotificationName其实就是NSString。要在Objective-C中使用Swift代码,类必须在这两种语言中都可用,并且不能是Swift结构体(比如通知或字符串)。

To do what you want, then, you need to have two extensions:

为了做你想做的事,你需要有两个扩展:

  • one for the Swift Notification.Name, as you have; and,
  • 一个用于Swift通知。名字,你有;而且,
  • one for an Objective-C object (NSString, say, or perhaps NSNotification if you prefer).
  • 一个对象是Objective-C对象(比如NSString,或者NSNotification,如果你喜欢的话)。

1) Add an Objective-C-compatible object extension to your Swift file:

1)在Swift文件中添加objective -c兼容的对象扩展:

public extension NSNotification {
    public static let blahblahblah: NSString = "blahblahblah"
}

Note: in Swift 4, properties must be computed for Objective-C compatibility. That would look like:

注意:在Swift 4中,必须为Objective-C兼容性计算属性。这样子:

public extension NSNotification {
    public static var blahblahblah: NSString {
        return "blahblahblah"
    }
}

Note the var in the computed property: computed properties can't be immutable, so can't use let.

注意computed属性中的var: computed properties不能是不可变的,所以不能使用let。

2) In the Objective-C file, import Xcode's generated Swift header file (below any other imports):

2)在Objective-C文件中,导入Xcode生成的Swift报头文件(在任何其他导入下):

#import "YourProjectName-Swift.h"

Note: replace YourProjectName with the actual name of your project. So, if your project is named "CoolGameApp", the Swift header would be "CoolGameApp-Swift.h". If your project name has spaces, like "Cool Game App", replace them with dashes: "Cool-Game-App-Swift.h"

注意:用项目的实际名称替换您的projectname。因此,如果你的项目被命名为“CoolGameApp”,那么Swift的标题将是“CoolGameApp-Swift.h”。如果你的项目名称有空格,如“Cool Game App”,用破折号:“Cool-Game-App- swift .h”代替。

3) Rebuild the project.

3)重建项目。

Now, you should be able to access the extension in Objective-C:

现在,你应该能够访问Objective-C中的扩展:

[[NSNotificationCenter defaultCenter] postNotificationName:NSNotification.blahblahblah object:self];

#2


5  

My extension in swift file

我在swift文件中的扩展名

extension Notification.Name {
    static let purchaseDidFinish = Notification.Name("purchaseDidFinish")
}

extension NSNotification {
    public static let purchaseDidFinish = Notification.Name.purchaseDidFinish
}

// OBJECTIVE-C

#import YourProjectName-Swift.h

[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(purchaseDidFinish) name:NSNotification.purchaseDidFinish object:nil];

// SWIFT
NotificationCenter.default.addObserver(self, selector: #selector(purchaseDidFinish), name: .purchaseDidFinish, object: nil)

@objc func purchaseDidFinish(notification: Notification) {
    print("purchaseDidFinish")
}

@leanne's answer was super helpful

@leanne的回答非常有帮助

#3


3  

In addition to the answers here, I had to add @objc to my NSNotification extension before my Obj-C code could see it (Swift 4).

除了这里的答案之外,我还必须将@objc添加到NSNotification扩展中,然后我的object - c代码才能看到它(Swift 4)。