Reputation: 7040
I'm in the midst of a rewrite and I'm at the point where I'm converting my Core Data models over to Swift. One such model, PointOfInterest
contains an enum
called types, which is used to identify the type of PointOfInterest
an object is:
@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
In Swift, the code looks like this:
// 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
}
The extension containing the enum
is in a different file (and I renamed the enum
values)
// PointOfInterest+Helpers.swift
extension PointOfInterest
{
@objc enum Types:Int
{
case General
case Restaurant
case TypeCount
}
}
In one of my original Objective-C Views, I have the following line (and a few others, but we'll concentrate on this one, as they all have the same error) which generates the error: Semantic issue: Use of undeclared identifier 'kGeneral'
poi.type = [NSNumber numberWithInt: kGeneral];
Even if I change kGeneral
to General
or even TypesGeneral
, nothing seems to work to access this enum
value from PointOfInterest
. The answer to this question doesn't help.
So my question is: how do I retrieve the value of a Swift class-level enum
from Objective-C code?
Upvotes: 0
Views: 422
Reputation: 535140
how do I retrieve the value of a Swift class-level enum from Objective-C code
You can't. Objective-C has no notion of an enum as being "inside" another class. Put the enum at top level if you want Objective-C to be able to see it.
Upvotes: 3