Objc>中定义的枚举在Swift>中声明用于Objc [英] enum defined in Objc > Declared in Swift > to be used in Objc

查看:168
本文介绍了Objc>中定义的枚举在Swift>中声明用于Objc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个情况.如果有人对此有解决方案,我将不胜感激

I have a situation. I would appreciated if anyone has a solution for this

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

这将引发错误-在类型MySwiftClass *的对象上找不到属性'abc'". 基本上,枚举不会作为"ProjectName-Swift.h"文件中的属性添加.

This is throwing an error - "Property ‘abc’ not found on object of type MySwiftClass *". Basically the enum is not added as property in the "ProjectName-Swift.h" file.

我相信正在发生的事情是,当我在Swift类中声明ObjC枚举时,它已转换为swift枚举,因此无法在ObjC文件中访问它.

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无效.

Note: Marking the Swift class as @objc did not work.

推荐答案

数字Swift可选内容无法在Objective-C中表示,因此不会在Objective-C中使用.声明abc不是可选的,它应该可以从Objective-C获得.

Numeric Swift optionals cannot be represented in Objective-C, and thus will not be exposed to Objective-C. Declare abc to not be optional and it should be available from Objective-C.

考虑此Objective-C枚举:

Consider this Objective-C enumeration:

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

然后考虑这个Swift 3类:

Then consider this Swift 3 class:

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

非可选的foo1将公开给Objective-C,而可选的foo2则不会公开.

The non-optional, foo1, will be exposed to Objective-C, whereas the optional, foo2, will not.

这篇关于Objc>中定义的枚举在Swift>中声明用于Objc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆