objective-c - xcode objective-c - 避免使用相同名称的多个方法(使用 'setEnabled' )

标签 objective-c ios xcode ios4 ios5

我在 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/

相关文章:

ios - 找不到从字符串插值段转换的成员

ios - 带有在方法内部创建的指针的ARC

ios - 如何将 subview 添加到 TableViewCell?

ios - 如何以编程方式在 uiview 上调整图像

objective-c - enum 在 Xcode 4.6 中表现得像一个 unsigned int,即使 enum 被定义为一个 signed int

ios - 如何在 iOS 中创建多行表格单元格?

xcode - 如何将Apple Pay整合到Xamarin Forms中?

ios - 推送 View 应以透明背景显示

Java 编程风格与 Objective C

ios - xcode 4.1 黄色三角形?