我在 XCode 中遇到错误:
发现多个名为“setEnabled”的方法具有不匹配的结果、参数类型或属性
我在 AppDelegate.m 中使用以下代码:
#import "BluetoothManager.h"
Class BluetoothManager = objc_getClass( "BluetoothManager" ) ;
id btCont = [BluetoothManager sharedInstance] ;
[btCont setEnabled:!currentState] ;
最佳答案
如果你正在导入 BluetoothManager.h
然后你在编译时解决了它,调整代码说:
#import "BluetoothManager.h"
Class BluetoothManagerClass = objc_getClass( "BluetoothManager" );
BluetoothManager *btCont = [BluetoothManagerClass sharedInstance];
[btCont setEnabled:!currentState];
您是否尝试支持一个新类,但也在较旧的 iOS 中运行?
对 Q1 的回应:“简单”版本,如果您不想做任何棘手的事情,就是放弃提供动态灵 active 的 objc_getClass
不经常需要
#import "BluetoothManager.h"
BluetoothManager *btCont = [BluetoothManager sharedInstance];
[btCont setEnabled:!currentState];
假设:
BluetoothManager
类在“BluetoothManager.h”中声明- 在其上声明(并定义)了一个类方法
+sharedInstance
(为了遵循常见的 Objective-C 命名约定,我将重命名为+sharedBluetoothManager
) - BluetoothManager 声明了一个名为
-setEnabled:
的方法
关于objective-c - xcode objective-c - 避免使用相同名称的多个方法(使用 'setEnabled' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8717009/