gpt4 book ai didi

ios:是否可以在运行时从同一类的两个定义类别中选择一个

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:16:35 24 4
gpt4 key购买 nike

我为同一个类定义了两个类别,它们具有相同的功能但定义不同。我想在某些条件下在运行时选择类别的特定功能。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()”的特定定义?

最佳答案

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

来自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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com