简体   繁体   中英

Using Objective-C extension classes in Swift

I am trying to write new features of my Objective-C project in Swift. I have an existing Objective-C file called NSString+Extensions defined like so

@interface NSString (Extensions) 

The file is named as NSString.Extensions.h. I have some application specific instance methods (ex: stringByRemovingSpecialCharacters). I have added a header file (bridging header), specific it in Swift Compiler - Code Generation in my project settings. I added the #import "NSString+Extensions.h" in that header file. But none of the methods in the extension file can be called from Swift file. How do I make use of this extension file in Swift? I have more extension files - NSData+Encryption.h etc, that I would like to use as well.

If you add a objective-C file to a swift project xcode will ask if you want to add an objective -c bridging header, to which you reply yes. You should then import your files in the bridging file and this will allow you to call them from your swift files. PS you can then delete the objective-C file which has no use now.

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