如何在类扩展中添加静态(存储)属性以生成单例? (迅速)

我想将此代码转换为Swift。 这里的Objective-C代码是一个单例对象(如果我可以这样描述)。 我可以使用dispatch_once_t来转换它,但我想使用一种更优雅的方式,它应该类似于“ static let bundle:NSBundle! ”。 但是,扩展中不允许使用“ static let bundle:NSBundle! ”,因为它不允许存储属性。

那么可以在没有dispatch_once_t的情况下转换代码吗?

我遇到了一个问题,我不能在类扩展中存储属性

@implementation NSBundle(CTFeedback)

+ (NSBundle *)feedbackBundle { static NSBundle *bundle = nil; static dispatch_once_t predicate; dispatch_once(&predicate, ^{ NSBundle *classBundle = [NSBundle bundleForClass:[CTFeedbackViewController class]]; NSURL *bundleURL = [classBundle URLForResource:@"CTFeedback" withExtension:@"bundle"]; if (bundleURL) { bundle = [NSBundle bundleWithURL:bundleURL]; } else { bundle = [NSBundle mainBundle]; } }); return bundle; } @end 

我的Swift代码:

 extension NSBundle { static func feedbackBundle()-> NSBundle { static let bundle: NSBundle! //!! **Compiler Error here** let classBundle = NSBundle.init(forClass: CTFeedbackViewController.self) let bundleURL = classBundle.URLForResource("CTFeedback", withExtension: "bundle") if let bundleURL2 = bundleURL { bundle = NSBundle(URL: bundleURL2) } else { bundle = NSBundle.mainBundle() } return bundle; } } 

更新:

感谢人们的回答。 我现在就这样做。 我不确定这是最好的方式/

 private class FeedbackBundle { static let classBundle = NSBundle.init(forClass: CTFeedbackViewController.self) } extension NSBundle { static func feedbackBundle()-> NSBundle { let bundleURL = FeedbackBundle.classBundle.URLForResource("CTFeedback", withExtension: "bundle") if let bundleURL2 = bundleURL { return NSBundle(URL: bundleURL2)! } else { return NSBundle.mainBundle() } } } 

在Swift中,您无法在扩展中添加静态变量。 如果可以访问,您可以在原始类中重试。 否则,您可以修改代码,如:

 if let bundleURL2 = bundleURL { return NSBundle(URL: bundleURL2) } else { return NSBundle.mainBundle() } 

您可以始终在扩展之外使用静态var驻留,可以在单独的类中,也可以作为简单的全局变量(无论如何都是静态变量)。

例如:

  private class FeedbackBundle { static var bundle: NSBundle! } extension NSBundle { static func feedbackBundle()-> NSBundle { let classBundle = NSBundle.init(forClass: CTFeedbackViewController.self) let bundleURL = classBundle.URLForResource("CTFeedback", withExtension: "bundle") if let bundleURL2 = bundleURL { FeedbackBundle.bundle = NSBundle(URL: bundleURL2) } else { FeedbackBundle.bundle = NSBundle.mainBundle() } return FeedbackBundle.bundle; } } 

您还可以子类化NSBundle并将此属性添加到它。