在 Swift 中使用 Objective-C 类别的正确方法是什么? [英] What's the proper way to use an Objective-C category within Swift?

查看:34
本文介绍了在 Swift 中使用 Objective-C 类别的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些类别方法导入到我的 Swift 文件中,但没有任何运气.

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

我希望自动完成显示 UIColor(hexInt: NSUInteger)UIColor(hexString: String)

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

推荐答案

实际上,您的类别转换为 Swift 如下:

Actually, your category is transtlated to Swift as follows:

extension UIColor {

    init(hex hexInt: Int) -> UIColor

    init(hexString: String) -> UIColor

}

因此,您应该使用:

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

let color = UIColor(hexString: "ffffff")

不过,测试版软件中的自动完成功能可能仍然存在问题.

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

这篇关于在 Swift 中使用 Objective-C 类别的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆