22

I'm trying to import some category methods into my Swift file without any luck.

ios-Bridging-Header.h:

#import "UIColor+Hex.h"

UIColor+Hex.h

#import <UIKit/UIKit.h>

@interface UIColor (Hex)

+ (UIColor *)colorWithHex:(NSUInteger)hexInt;
+ (UIColor *)colorWithHexString:(NSString *)hexString;

@end

I would expect the autocomplete to reveal UIColor(hexInt: NSUInteger) and UIColor(hexString: String)

sevenflow
  • 767
  • 1
  • 6
  • 21
  • https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/Extensions.html – holex Jun 05 '14 at 15:54
  • I have got a couple of questions about your code: In your bridging header should it not be "#import" instead of "import" rather? Which line did you write to finally import your category in Swift? – Captain Fim Jun 27 '14 at 09:00
  • Yep, typo in my question. The code I accepted below worked well for me! – sevenflow Jul 01 '14 at 15:18
  • See my new answer below. Basically you need a bridging header: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html – Dan Rosenstark Dec 21 '14 at 17:15
  • I'm trying to use NSAttributedString extension (cocopods), i've added them in bridging header file as well but same issue as yours. I expect autocomplete to reveal the methods but it's not.. Pods i'm using is from: https://github.com/dbowen/NSAttributedString-DDHTML Thanks, – Fayza Nawaz Nov 10 '15 at 07:19

2 Answers2

35

Actually, your category is transtlated to Swift as follows:

extension UIColor {

    init(hex hexInt: Int) -> UIColor

    init(hexString: String) -> UIColor

}

And because of that, you should be using:

let color = UIColor(hex: 0xffffff) // instead of hexInt:

let color = UIColor(hexString: "ffffff")

Autocompletion may still be buggy in the beta software, though.

akashivskyy
  • 44,342
  • 16
  • 106
  • 116
25

You can use Objective-C categories directly in Swift. This gets really interesting for some of the bridged classes, like String. Extend NSString with a category in Objective-C, and then you can access it from Swift (directly on String!)

The way to do this is by creating a "bridging header" in your Swift project.

Full instructions here.

The short of it is:

  1. Make a .h header file (in Objective-C) with all the other #import statements in it
  2. Put the path to that file in Objective-C Bridging Header in your Build Settings
  3. No need to import the bridging header in your Swift file. It's already there
Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421
  • but the problem is this category will be available in all swift file, however in ObjC we can use in file where actually it is required – Mayank Jain May 23 '17 at 09:08
  • 1
    @MayankJain more importantly, in Objective-C you can choose between different categories that have similar method names. However, this is probably a good way to shoot yourself in the foot... perhaps Swift it ultimately on the right side of this thing. – Dan Rosenstark May 23 '17 at 14:42
  • @DanRosenstark agreed...!!! In one of my project it is my requirement to import category file in specific file, I wanted other files should use original implementation and only specific file can use custom category. Category is written in ObjC. – Mayank Jain May 24 '17 at 05:21