OGeek|极客世界-中国程序员成长平台

标题: ios:是否可以在运行时从同一类的两个定义类别中选择一个 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 12:04
标题: ios:是否可以在运行时从同一类的两个定义类别中选择一个

我为同一个类定义了两个类别,并且具有相同的功能但定义不同。我想在某些条件下在运行时选择一个类别的特定功能。 obj c 编译器是否为类别维护一些簿记。

我在考虑 C++(虚拟函数/Vtable),这些可以使用多态性来实现。如何在 objective-c 中解决这种情况。

Class MyClass;

File:Myclass+category1.h
@interface MyClass (CategoryOne)
-(void) printCategory()
@end 

File:Myclass+category2.h
@interface MyClass (CategoryTwo)
-(void) printCategory()
@end 

现在我在 MyClass.m 中包含了这两个头文件。是否有可能在某些运行时条件下自由选择“printCategory()”的特定定义?



Best Answer-推荐答案


没有。如果两个类别实现相同的方法,则不确定执行哪一个。

来自 docs :

There’s no limit to the number of categories that you can add to a class, but each category name must be different, and each should declare and define a different set of methods.

关于ios:是否可以在运行时从同一类的两个定义类别中选择一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9606878/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4