ios - 自定义类 "Cannot find interface declaration"中无法识别 Objective-C 类别方法

标签 ios objective-c class categories

我想在 View Controller 的自定义类中使用自定义方法

//Viewcontroller.h
#import "Class1.h"

//Class1.h
//#import "Class1+Category.h" // Deemed unnecessary in comments below.
@interface Class1: NSObject
-(void)doSomething;
@end

//Class1.m
#import "Class1.h"
@implementation Class1
-(void)doSomething{
  NSLog("In doSomething");
}
@end

现在我想要一个 Class1 的类别方法。
//Class1+Category1.h
#import "Class1.h"
@interface Class1  (Category1) // ERROR : Cannot find interface declaration
-(void)doAnotherThing;
@end

//Class1+Category1.m
#import "Class1+Category.h"
@implementation Class1 (Category1)
-(void)doAnotherThing{
  NSLog(@"Did Another thing");
}
@end

最后 - 在我的 viewcontroller.m 中,我看到了 doSomething 方法,但没有看到 doAnother 方法
 //viewcontroller.m
 Class1 *myClass1 = [[Class1 alloc]init];
 [Class1 doSomething]; //Works great!
 [Class1 doAnotherThing]; //Not recognized

我已将 -all_load 添加到我的目标设置中。我没有想法..我使用@class吗?我收到“找不到接口(interface)声明”错误

最佳答案

您的类和类别乍一看似乎是正确的,但您的 Controller 需要导入 Class1+Category.h。也许这就是你错过的?

关于ios - 自定义类 "Cannot find interface declaration"中无法识别 Objective-C 类别方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15934908/

相关文章:

ios - 无法在格式转换器音频单元上设置格式

ios - iOS 中模糊的半透明导航栏

iPhone 数据表单提交最佳实践?

objective-c - OS X如何申请 "clean"内存?

javascript - 如何获取连接的wifi网络名称

objective-c - 在运行时运行任意代码

javascript - Cordova window.open _self 不工作但 _blank 工作

c++ - 附加冒号在模板类中意味着什么。类名<T, SIZE>::类名:

Java : If A extends B and B extends Object, 是多重继承

c++ - vector 是否在 push_back() 上重新创建所有对象?