Swift: How to call a category or class method from Objective-C

RawMean

I have a category on UIImage that is written in Objective-C. The following are some example methods. How do I call these methods in Swift?

+(UIImage *) imageOrPDFNamed:(NSString *)resourceName; 
+(UIImage *) imageWithPDFNamed:(NSString *)resourceName;

I have tried the following and it does not recognize the methods:

let image = UIImage(imageOrPDFNamed:"test.pdf")
let image = UIImage(PDFNamed:"test.pdf")
codester

There are various cases for categories function and it's calling conventions.

1)In swift class functions in categories can be imported as convenience initializes if they returned same type on which they called like

 +(UIImage *) imageOrPDFNamed:(NSString *)resourceName ;

so it can be called as

    UIImage(orPDFNamed: "abc")

2)However if class functions in categories are not returning same object type(UIImage) on which it calls in this case UIImage like

   +(int) imageOrPDFNamedInt:(NSString *)resourceName ;

it will called as

    UIImage.imageOrPDFNamedInt("abc")

3)If categories function is not class function so it will call as directly on instance of UIImage like

   -(UIImage *) imagePDFNamed:(NSString *)resourceName ;

called as

    UIImage().imagePDFNamed("abc")

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Objective-C Class Method

来自分类Dev

Call method in different class from click event method in c#

来自分类Dev

Forcing a subclass to call its superclass method in Objective-C

来自分类Dev

iOS Swift错误-Objective C方法...由method提供

来自分类Dev

Objective-C - Is the Pointer to a Class different from a pointer to another Class?

来自分类Dev

Call derived class method from base class instance

来自分类Dev

How to call a varargs method with an additional argument from a varargs method

来自分类Dev

C# Inheritance Prevent Base Class Method Call Derived Class method

来自分类Dev

学习Objective-C或Swift

来自分类Dev

与Swift集成Objective C协议

来自分类Dev

从Objective-C到Swift

来自分类Dev

Swift中的Objective C委托

来自分类Dev

从Objective-C到Swift

来自分类Dev

how to use uiswitch for selecting category in swift?

来自分类Dev

how to call service method from object property in angularjs?

来自分类Dev

how to call the web method only when '/' is the last character from autocomplete?

来自分类Dev

符合Objective-C协议的Swift类

来自分类Dev

在Objective-C中使用Swift协议

来自分类Dev

Objective-C类中的Swift协议

来自分类Dev

在Swift中实现Objective C协议

来自分类Dev

等效的Objective-C setFrame Swift

来自分类Dev

Swift nil与Objective-C nil不同

来自分类Dev

Swift类扩展了Objective C委托

来自分类Dev

Swift-Objective-C加载类方法?

来自分类Dev

从Objective-C访问Swift扩展

来自分类Dev

swift init not visible in objective-C

来自分类Dev

在Swift中捕获Objective-C异常

来自分类Dev

Swift与Objective-C:应用性能

来自分类Dev

使用Swift处理Objective-C块