Objective-C enum在Swift中不可见

时间:2022-09-07 09:42:29

I have an enum which is like this declared in my objective-c header file:

我在objective-c头文件中声明了一个enum

typedef NS_ENUM(NSInteger, FontSize) {
    VerySmall = 12,
    Small = 14,
    Medium = 16,
    Big = 18
};

Then in my bridging header I import this header.

然后在我的桥接头中导入这个头。

from my swift code, when I try to declare 'FontSize' as parameter, the compiler says 'Use of undeclared type FontSize'.

在我的swift代码中,当我试图将“FontSize”声明为参数时,编译器会说“使用未声明的类型FontSize”。

From the developer guide, this should be possible. Anyone experiencing the same problem?

从开发人员指南来看,这应该是可能的。有人遇到同样的问题吗?

3 个解决方案

#1


4  

Start over with a clean Swift project, add a single .h file (accept the automatic creation of Bridging-Headers)

从一个干净的Swift项目开始,添加一个.h文件(接受自动创建桥头)

Objective-C FontSize.h

objective - c FontSize.h

typedef NS_ENUM(NSInteger, FontSize) {
    VerySmall = 12,
    Small = 14,
    Medium = 16,
    Big = 18
};

Bridging-Header

Bridging-Header

#import "FontSize.h"

Swift Implementation

迅速实现

import UIKit
class ViewController: UIViewController {
    let fontSize:FontSize = .VerySmall
}

Built, linked, ran & tested on Xcode 6.4 & 7.0.

在x6.4 & 7.0上构建、链接、运行和测试。

#2


2  

I had the same issue and resolved it by doing BOTH of the following:

我也有同样的问题,通过以下两种方式来解决:

  1. Move the enum declaration to outside the @interface block
  2. 将enum声明移动到@interface块之外
  3. Remove the period '.' from the enum reference in the Swift code
  4. 删除这段时间”。从《Swift代码》中的enum引用。

let fontSize:FontSize = VerySmall

让字形大小:字形大小=很小

#3


1  

I still couldn't see my enums even with NS_ENUM answer above.

即使上面有NS_ENUM答案,我仍然看不到我的enum。

It turns out there was a change in XCode 7.3 where NS_ENUMs have to be defined outside the @interface-@end block.

在XCode 7.3中,NS_ENUMs必须在@interface-@end块之外定义。

Calling obj-c enum from swift not working after upgrading to Xcode 7.3 swift 2.2

在升级到Xcode 7.3 swift 2.2之后,从swift调用object -c enum不会工作

#1


4  

Start over with a clean Swift project, add a single .h file (accept the automatic creation of Bridging-Headers)

从一个干净的Swift项目开始,添加一个.h文件(接受自动创建桥头)

Objective-C FontSize.h

objective - c FontSize.h

typedef NS_ENUM(NSInteger, FontSize) {
    VerySmall = 12,
    Small = 14,
    Medium = 16,
    Big = 18
};

Bridging-Header

Bridging-Header

#import "FontSize.h"

Swift Implementation

迅速实现

import UIKit
class ViewController: UIViewController {
    let fontSize:FontSize = .VerySmall
}

Built, linked, ran & tested on Xcode 6.4 & 7.0.

在x6.4 & 7.0上构建、链接、运行和测试。

#2


2  

I had the same issue and resolved it by doing BOTH of the following:

我也有同样的问题,通过以下两种方式来解决:

  1. Move the enum declaration to outside the @interface block
  2. 将enum声明移动到@interface块之外
  3. Remove the period '.' from the enum reference in the Swift code
  4. 删除这段时间”。从《Swift代码》中的enum引用。

let fontSize:FontSize = VerySmall

让字形大小:字形大小=很小

#3


1  

I still couldn't see my enums even with NS_ENUM answer above.

即使上面有NS_ENUM答案,我仍然看不到我的enum。

It turns out there was a change in XCode 7.3 where NS_ENUMs have to be defined outside the @interface-@end block.

在XCode 7.3中,NS_ENUMs必须在@interface-@end块之外定义。

Calling obj-c enum from swift not working after upgrading to Xcode 7.3 swift 2.2

在升级到Xcode 7.3 swift 2.2之后,从swift调用object -c enum不会工作