我的问题的一个简单例子:
“在 BlahDataController.h 中”
@interface BlahDataController : NSObject
-(NSString *)aMethod:(NSString *)theString;
@end
“在 BlahDataController.m 中”
#import "BlahDataController.h"
@implementation BlahDataController
-(NSString *)aMethod:(NSString *)theString
{
return @"Something";
}
@end
“在 BobViewController.h 中”
@interface BobViewController : NSObject
-(void)aMethodOfSomeSort;
@end
“在 BobViewController.m 中”
#import "BobViewController.h"
#import "BlahDataController.h"
@implementation BobViewController
-(void)aMethodOfSomeSort
{
BlahDataController *blahDataController = [[BlahDataController alloc] init];
NSLog(@"%@",[blahDataController aMethod:@"Variable"]);
}
@end
在行 "NSLog(@"%@",[blahDataController aMethod:@"Variable"]);"我收到错误消息:“'BlahDataController' 没有可见的@interface 声明选择器'aMethod:'”
有人知道为什么会出现这个错误吗?
-=-=-=-=-=-=-=-=-=-=-
问题是,在我的实际程序中,我有相同的实现,并且它适用于以这种方式创建的数百种方法。但是,每隔一段时间,我会在新创建的方法上收到此错误。我没有做任何不同的事情。它只是不会承认它是新创建的存在。
-=-=-=-=-=-=-=-=-=-=-
这就是我目前的处理方式,虽然我不知道为什么编译器接受这种方式,但不接受另一种方式:
修改BobViewController.m:
#import "BobViewController.h"
#import "BlahDataController.h"
#import "AnotherDataController.h"
@implementation BobViewController
-(void)aMethodOfSomeSort
{
BlahDataController *blahDataController = [[BlahDataController alloc] init];
AnotherDataController *anotherDataController = [[AnotherDataController alloc] init];
[anotherDataController fixedMethod:blahDataController theString:@"Variable"];
}
@end
“在 AnotherDataController.h 中”
@interface AnotherDataController : NSObject
-(void)fixedMethod:(BlahDataController *)blahDataController theString:(NSString *)theString;
@end
“在 AnotherDataController.m 中”
#import "AnotherDataController.h"
#import "BlahDataController.h"
@implementation AnotherDataController
-(void)fixedMethod:(BlahDataController *)blahDataController theString:(NSString *)theString
{
NSLog(@"%@",[blahDataController aMethod:theString]);
}
@end
而且......它工作得很好......所以我想 xcode 只是无法识别一个类中的方法,并且在另一个类中正常工作......伙计,我不知道为什么会出现这个错误发生...
-=-=-
次要更新:
完成整个“xcode 舞蹈”并没有解决问题
1) 清理构建
2) 删除派生数据
3)完全关闭XCode并重新打开
最佳答案
tl;dr - 项目中某处存在重复文件!去追杀它,毫不留情地摧毁它!
好的,对于所有将来遇到这个问题的人;这就是问题所在。
我几个月前制作了 BlahDataController。大约一周前,我重组了项目的文件夹并将 BlahDataController 从名为“Blah”的文件夹移动到另一个名为“Data”的文件夹。
当我在“数据”文件夹中更改 BlahDataController 的代码时,我的一个类可以看到更改后的代码,但是,另一个类不能。
最终的问题是,当我移动 BlahDataController 时,它实际上创建了它的一个副本。所以我在“数据”文件夹中有一个 BlahDataController,在“Blah”文件夹中有一个较旧的 BlahDataController。尽管较旧的 BlahDataController 不再附加到项目管理器中的项目(xcode 的左侧),但物理文件仍然存在于文件夹中这一事实导致了这个问题。
删除旧的 BlahDataController 副本后,问题得到解决。
关于objective-c - "No visible @interface for ' BlahDataController ' declares the selector ' aMethod : '",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10727677/