如何将@class前向声明转换为Swift? [英] How to translate @class forward declaration to Swift?

查看:436
本文介绍了如何将@class前向声明转换为Swift?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Objective-C的AppDelegate中.

I have in my AppDelegate in Objective-C.

@class PhoneViewController;

@interface AppDelegate : NSObject <UIApplicationDelegate> {

    IBOutlet UINavigationController *numberActivationFlow;
    IBOutlet UINavigationController *phoneFlow;
    IBOutlet PhoneViewController *phoneViewController;
    IBOutlet UINavigationController *configurationWizardFlow;
    IBOutlet UIViewController *configurationWizardFlowRoot;

    IBOutlet NumberActivationViewController *numberActivationViewController;

    NSManagedObjectModel *managedObjectModel;
    NSManagedObjectContext *managedObjectContext;       
    NSPersistentStoreCoordinator *persistentStoreCoordinator;

    IBOutlet UINavigationItem *captionNavigationItem;

    BOOL initialized;
    BOOL firstTimeExecCompleted;
    BOOL firstTimeActive;
    BOOL started;
    Activation* activation;
    Variant* variant;
}

+ (AppDelegate*) sharedInstance;

我正在将上述类翻译为Swift,并且我想知道如何翻译以下行:

I am translating the above class to Swift and I wish to know how to translate the following line:

@class PhoneViewController;

特别是上面的代码@class PhoneViewController;出现问题.因为它不是变量,我该如何用Swift编写代码?

Specially I have a problem the above code @class PhoneViewController;. How can I write in Swift because it is not a variable?

推荐答案

如果您不拥有代码,就不能总是忽略@class转发声明.

错误"报告:

我使用的是Objective-C库,该库作为不透明的第三方框架使用,仅公开头文件.发生错误时,框架将生成MyError对象并将其出售给委托.到目前为止,一切都很好.

You Cannot always ignore the @class forward declarations if you do not own the code.

'Bug' Report:

I work with an Objective-C library that is consumed as an opaque third-party framework, with only the header files exposed. When errors occur, the framework generates and vends a MyError object to a delegate. So far, so good.

由于使用该框架的开发人员从不创建此类的对象,因此可以公开,但无需担心;因为它是预先声明的,所以很好-在ObjC中!但是Swift没有前向声明,并且该对象在依赖ObjC框架的Swift文件中被忽略.在这种情况下,没有MyError对象意味着没有错误报告!

Since the developer using the framework never creates an object of this class, it is not exposed, but no worries; since it is forward-declared, it's fine -- in ObjC! But Swift does not have Forward Declarations, and the object was ignored in Swift files that rely on the ObjC framework. In this case, no MyError object means no error reporting!

我通过在Swift文件中重新声明此问题来解决此问题,例如"MyError + Workaround.swift".

I worked around the issue by redeclaring it in a Swift file, e.g. "MyError+Workaround.swift".

import Foundation

class MyError: NSError {} // the object actually is an NSError subclass

就是这样.希望这可以帮助其他人.

That's it. Hope this helps someone else out there.

这篇关于如何将@class前向声明转换为Swift?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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