如何从Objective-C代码访问Swift类级枚举

时间:2015-06-16 17:32:54

标签: ios objective-c swift

我正在进行重写,而且我正在将我的核心数据模型转换为Swift。其中一个模型PointOfInterest包含一个名为 types enum,用于标识对象{​​{1}}的类型:

PointOfInterest

在Swift中,代码如下所示:

@class Port;

@interface PointOfInterest : NSManagedObject

enum types {
    kGeneral,
    kRestaurant,
    kTypeCount
};

@property (nonatomic, retain) NSNumber * averageRating;
@property (nonatomic, retain) NSString * city;
@property (nonatomic, retain) NSString * countryName;
@property (nonatomic, retain) NSNumber * id;
@property (nonatomic, retain) NSString * imageURL;
@property (nonatomic, retain) NSNumber * latitude;
@property (nonatomic, retain) NSNumber * longitude;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * phone;
@property (nonatomic, retain) NSString * postalCode;
@property (nonatomic, retain) NSString * priceLevel;
@property (nonatomic, retain) NSNumber * rankingNumber;
@property (nonatomic, retain) NSString * rankingString;
@property (nonatomic, retain) NSString * state;
@property (nonatomic, retain) NSString * street1;
@property (nonatomic, retain) NSString * street2;
@property (nonatomic, retain) NSNumber * totalReviews;
@property (nonatomic, retain) NSNumber * type;
@property (nonatomic, retain) NSString * webURL;
@property (nonatomic, retain) NSSet *ports;
@end

@interface PointOfInterest (CoreDataGeneratedAccessors)

- (void)addPortsObject:(Port *)value;
- (void)removePortsObject:(Port *)value;
- (void)addPorts:(NSSet *)values;
- (void)removePorts:(NSSet *)values;

@end

包含// PointOfInterest.swift class PointOfInterest: NSManagedObject { @NSManaged var averageRating: NSNumber @NSManaged var city: String @NSManaged var countryName: String @NSManaged var id: NSNumber @NSManaged var imageURL: String @NSManaged var latitude: NSNumber @NSManaged var longitude: NSNumber @NSManaged var name: String @NSManaged var phone: String @NSManaged var postalCode: String @NSManaged var priceLevel: String @NSManaged var rankingNumber: NSNumber @NSManaged var rankingString: String @NSManaged var state: String @NSManaged var street1: String @NSManaged var street2: String @NSManaged var totalReviews: NSNumber @NSManaged var type: NSNumber @NSManaged var webURL: String @NSManaged var ports: NSSet } 的扩展程序位于不同的文件中(我重命名为enum值)

enum

在我最初的一个Objective-C视图中,我有以下一行(以及其他几个,但我们将专注于这一行,因为它们都有相同的错误),这会生成错误:{{1} }

// PointOfInterest+Helpers.swift
extension PointOfInterest
{
    @objc enum Types:Int
    {
        case General
        case Restaurant
        case TypeCount
    }

}

即使我将Semantic issue: Use of undeclared identifier 'kGeneral'更改为poi.type = [NSNumber numberWithInt: kGeneral]; 甚至kGeneral,似乎无法从General访问此TypesGeneral值。 this question的答案无济于事。

所以我的问题是:如何从Objective-C代码中检索Swift类级enum的值?

1 个答案:

答案 0 :(得分:2)

  

如何从Objective-C代码

中检索Swift类级别枚举的值

你做不到。 Objective-C没有将枚举作为另一个类“内部”的概念。如果您希望Objective-C能够看到它,请将枚举放在顶层。