简体   繁体   English

在Objc中定义的枚举>在Swift中声明>在Objc中使用

[英]enum defined in Objc > Declared in Swift > to be used in Objc

I have a situation. 我有情况 I would appreciated if anyone has a solution for this 如果有人对此有解决方案,我将不胜感激

  • I have an objC enum say Abc 我有一个objC enumAbc
  • I declare this in a swift class, say, MySwiftClass.swift as var abc : Abc! 我在swift类中声明了这一点,比如说MySwiftClass.swiftvar abc : Abc!
  • I have created an instance of MySwiftClass ( mySwiftClass ) in another ObjC class (myObjC.m file) 我在另一个ObjC类(myObjC.m文件)中创建了MySwiftClass( mySwiftClass )的实例。
  • In myObjC.m, I'm trying to access enum Abc as mySwiftClass.abc . 在myObjC.m中,我尝试以mySwiftClass.abc身份访问枚举Abc。

This is throwing an error - “Property 'abc' not found on object of type MySwiftClass *”. 这将引发错误-“在MySwiftClass *类型的对象上找不到属性'abc'。 Basically the enum is not added as property in the “ProjectName-Swift.h” file. 基本上,枚举不作为属性添加到“ ProjectName-Swift.h”文件中。

What I believe is happening is that when I'm declaring the ObjC enum in Swift class, it is getting converted to a swift enum and hence I'm not able to access it in ObjC file. 我相信正在发生的事情是,当我在Swift类中声明ObjC枚举时,它已转换为swift枚举,因此无法在ObjC文件中访问它。

Note: Marking the Swift class as @objc did not work. 注意:将Swift类标记为@objc无效。

Numeric Swift optionals cannot be represented in Objective-C, and thus will not be exposed to Objective-C. Swift中的数字可选内容无法在Objective-C中表示,因此不会暴露于Objective-C。 Declare abc to not be optional and it should be available from Objective-C. 声明abc不是可选的,应该可以从Objective-C获得。

Consider this Objective-C enumeration: 考虑以下Objective-C枚举:

typedef NS_ENUM(NSInteger, Foo) {
    FooBar,
    FooBaz,
    FooQux
};

Then consider this Swift 3 class: 然后考虑这个Swift 3类:

class SomeObject: NSObject {
    var foo1: Foo  = .bar          // this is exposed to Objective-C
    var foo2: Foo! = .bar          // this is not
}

The non-optional, foo1 , will be exposed to Objective-C, whereas the optional, foo2 , will not. 非可选的foo1将公开给Objective-C,而可选的foo2则不会公开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM