2016-04-12 23 views
2

说我有斯威夫特便利初始化:是否可以调用雨燕方便初始化在Objective-C

extension UIImage { 
    convenience init?(bundleNamed name: String) { 
     let bundle = NSBundle(forClass: Foo.self) 
     self.init(named: name, inBundle: bundle, compatibleWithTraitCollection: nil) 
    } 
} 

我怎么可能把这个在Objective-C?以下操作不起作用:

[UIImage bundleNamed:@"Bar"]; 
[[UIImage alloc] initWithBundleNamed:@"Bar"]; 

我是否需要额外的类扩展专门用于Objective-C?


解决方案:请按照以下拉塞的回答,我必须做的步骤是:

在Objective-C类实现文件,添加

#import <ModuleName-Swift.h> 

然后我不得不删除派生数据和重建。然后,我能够使用的便利初始化如下:

[[UIImage alloc] initWithBundleNamed: @"Bar"]; 

我并不需要声明初始化为public因为internal默认级别是足够我的模块。

+0

是什么方法'initWithFoo的代码: '?只要有'[Fooable foo:@“Bar”];'表明这是一个级别的方法,而不是实例级别的方法 – Popeye

回答