我正在开发一个混合了 swift和obj-c代码的ios应用程序.我的一个obj-c模型类定义了一个包含字符串的结构,以帮助转换为字典并返回.我有桥接头设置,我可以在swift类中访问我的 objective-c类中
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
我以为我只能使用
OrderItem.OrderItemAttributes.created
访问属性字符串,但swift不接受该语法.有没有办法做我想做的事情而不对我的objective-c代码进行重大改变?
变量OrderItemAttributes不是OrderItem名称空间的一部分.它将直接访问为:var foo: NSString = OrderItemAttributes.created.takeUnretainedValue()
您看到自动完成的问题是因为OrderItemAttributes不明确;它既是类型名称,也是变量名称.对结构类型名称和全局变量使用不同的名称以避免歧义.例如,在类型名称的末尾添加“Struct”:
extern const struct OrderItemAttributesStruct { __unsafe_unretained NSString *created; __unsafe_unretained NSString *created_by_id; __unsafe_unretained NSString *device_deleted; } OrderItemAttributes;