【问题标题】:xcode objective-c - avoiding multiple methods with the same name (using 'setEnabled')xcode objective-c - 避免使用相同名称的多个方法(使用'setEnabled')
【发布时间】:2012-01-03 18:35:59
【问题描述】:

我在 XCode 中遇到错误:

发现多个名为“setEnabled”的方法的结果、参数类型或属性不匹配

我在 AppDelegate.m 中使用以下代码:

#import "BluetoothManager.h"
Class BluetoothManager = objc_getClass( "BluetoothManager" ) ;
id btCont = [BluetoothManager sharedInstance] ;
[btCont setEnabled:!currentState] ;

【问题讨论】:

  • 为什么要这样做?
  • 我不知道 - 我刚刚在 Stack 上找到了一些代码,据说可以让我做我需要做的事情。不幸的是,我不太擅长使用 XCode 进行编程。

标签: objective-c ios xcode ios4 ios5


【解决方案1】:

如果您要导入 BluetoothManager.h,那么您已经在编译时解决了它,请将代码调整为:

#import "BluetoothManager.h"
Class BluetoothManagerClass = objc_getClass( "BluetoothManager" );
BluetoothManager *btCont = [BluetoothManagerClass sharedInstance];
[btCont setEnabled:!currentState];

您是否尝试支持新类,但也可以在旧版 iOS 中运行?

针对 Q1:“简单”版本,如果您不想做任何棘手的事情,那就是放弃 objc_getClass,它提供了不经常需要的动态灵活性 p>

#import "BluetoothManager.h"
BluetoothManager *btCont = [BluetoothManager sharedInstance];
[btCont setEnabled:!currentState];

假设:

  • BluetoothManager 类在“BluetoothManager.h”中声明
  • 在其上声明(并定义)了一个类方法 +sharedInstance(为了遵循常见的 Objective-C 命名约定,我将重命名为 +sharedBluetoothManager
  • BluetoothManager 声明了一个名为-setEnabled: 的方法

【讨论】:

  • 好吧,这个应用程序真正要做的就是在某人的本地手机上运行,​​所以不 - 它不需要支持任何旧版本的 iOS - 它是一个允许一键切换蓝牙的应用程序.当我切换你的代码时,我收到一个错误:“使用未声明的标识符'btCont'” - 也是“BluetoothManagerClass”应该是“BluetoothManager”
  • @bshirly 为什么我会收到一条评论说 btCont 是一个未声明的标识符?
  • 需要更多代码(BluetoothManager 标头)来猜测
猜你喜欢
  • 2013-05-21
  • 2013-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-25
  • 2012-01-18
  • 2016-07-25
  • 1970-01-01
相关资源
最近更新 更多