定制类“找不到接口声明”中无法识别的目标C类别方法

mat*_*tyd 1 class objective-c categories ios

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

//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
Run Code Online (Sandbox Code Playgroud)

现在我想要一个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
Run Code Online (Sandbox Code Playgroud)

最后-在我的viewcontroller.m文件中,我看到了doSomething方法,但没有看到doAnother

 //viewcontroller.m
 Class1 *myClass1 = [[Class1 alloc]init];
 [Class1 doSomething]; //Works great!
 [Class1 doAnotherThing]; //Not recognized
Run Code Online (Sandbox Code Playgroud)

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

Tau*_*aum 5

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