gpt4 book ai didi

swift - 在 Swift 中使用 Objective-C 类别的正确方法是什么?

转载 作者:IT王子 更新时间:2023-10-29 05:14:56 24 4
gpt4 key购买 nike

我试图将一些类别方法导入到我的 Swift 文件中,但没有成功。

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)

最佳答案

实际上,您的类别被翻译成 Swift 如下:

extension UIColor {

init(hex hexInt: Int) -> UIColor

init(hexString: String) -> UIColor

}

因此,您应该使用:

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

let color = UIColor(hexString: "ffffff")

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

关于swift - 在 Swift 中使用 Objective-C 类别的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24064627/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com