I'm working on an ios application that has a mix of swift and obj-c code. One of my obj-c model classes defines a struct containing strings to assist in converting to a dictionary and back. I have the bridging header setup and I can access methods defined in my objective-c class in a swift class. What I can't figure out is how to access the static stuct to get at my property strings. Here is a snippet of my .h and .m files:
OrderItem.h
extern const struct OrderItemAttributes {
__unsafe_unretained NSString *created;
__unsafe_unretained NSString *created_by_id;
__unsafe_unretained NSString *device_deleted;
} OrderItemAttributes;
@interface OrderItem : NSManagedObject {}
@property (nonatomic, strong) NSDate* created;
@end
OrderItem.m
const struct OrderItemAttributes OrderItemAttributes = {
.created = @"created",
.created_by_id = @"created_by_id",
.device_deleted = @"device_deleted",
};
@implementation OrderItem
@dynamic created;
@end
I thought I would simply be able to use
OrderItem.OrderItemAttributes.created
to access the attribute strings but swift doesn't accept that syntax. Is there any way to do what I want without major changes to my objective-c code?
The variable OrderItemAttributes
isn't part of the OrderItem
namespace. It would be accessed directly as:
var foo: NSString = OrderItemAttributes.created.takeUnretainedValue()
The problem you're seeing with autocomplete occurs because OrderItemAttributes
is ambiguous; it's both a type name and a variable name. Use different names for the struct type name and the global variable to avoid the ambiguity. Eg, add 'Struct' to the end of the type name:
extern const struct OrderItemAttributesStruct {
__unsafe_unretained NSString *created;
__unsafe_unretained NSString *created_by_id;
__unsafe_unretained NSString *device_deleted;
} OrderItemAttributes;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.