从Swift访问objective-c Struct
发布时间:2020-12-14 04:30:43 所属栏目:百科 来源:网络整理
导读:我正在开发一个混合了 swift和obj-c代码的ios应用程序.我的一个obj-c模型类定义了一个包含字符串的结构,以帮助转换为字典并返回.我有桥接头设置,我可以在swift类中访问我的 objective-c类中定义的方法.我无法弄清楚的是如何访问静态stuct以获取我的属性字符
我正在开发一个混合了
swift和obj-c代码的ios应用程序.我的一个obj-c模型类定义了一个包含字符串的结构,以帮助转换为字典并返回.我有桥接头设置,我可以在swift类中访问我的
objective-c类中定义的方法.我无法弄清楚的是如何访问静态stuct以获取我的属性字符串.这是我的.h和.m文件的片段:
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 我以为我只能使用
访问属性字符串,但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; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |