非常基本的XCode Objective-C问题

时间:2022-11-06 22:24:18

I'm making a simple program in Objective-C. It has one class with a lot of methods. I'd just like to put the methods in another file... so I could move the following

我在Objective-C中制作了一个简单的程序。它有一个类有很多方法。我只想将这些方法放在另一个文件中...所以我可以移动以下内容

- (void) myfunc1 {...}
- (void) myfunc2 {...}
// more functions

to another file and replace the above w/ something like

到另一个文件并替换上面的东西像

#include "myNewFile.something"

I'm fine w/ putting the #include (or whatever) statement just right in the original file.

我很好地将#include(或其他)语句恰好放在原始文件中。

How can I do this?

我怎样才能做到这一点?

2 个解决方案

#1


You'll need to split your methods in to different "categories," which each get their own h and m files. You'll also need an h and m file for the class itself. Here's a simple little example that I think will show you what you need to do.

您需要将方法拆分为不同的“类别”,每个类别都有自己的h和m文件。你还需要一个h和m文件来为类本身。这是一个简单的小例子,我认为它会告诉你你需要做什么。

TestClass.h

#import <Cocoa/Cocoa.h>
@interface TestClass : NSObject {
}
@end

TestClass.m

#import "TestClass.h"
@implementation TestClass
@end

TestClass+Category1.h

#import <Cocoa/Cocoa.h>
#import "TestClass.h"
@interface TestClass(Category1) 
-(void)TestMethod1;
@end

TestClass+Category1.m

#import "TestClass+Category1.h"
@implementation TestClass(Category1)
-(void)TestMethod1 {
NSLog(@"This is the output from TestMethod1");
}
@end

TestClass+Category2.h

#import <Cocoa/Cocoa.h>
#import "TestClass.h"
@interface TestClass(Category2) 
-(void)TestMethod2;
@end

TestClass+Category2.m

#import "TestClass+Category2.h"
@implementation TestClass(Category2)
-(void)TestMethod2 {
NSLog(@"This is the output from TestMethod2");
}
@end

Then in whatever file is using your class, you'll use

然后在任何使用你的课程的文件中,你将使用

#import "TestClass.h"
#import "TestClass+Category1.h"
#import "TestClass+Category2.h"

Now you can create an instance of class TestClass and it will have all the methods from both category1 and category2. Simply use

现在,您可以创建类TestClass的实例,它将包含category1和category2中的所有方法。简单地使用

TestClass* test = [[TestClass alloc] init];
[test TestMethod1];
[test TestMethod2];

#2


You need to create a header file and include it as appropriate.

您需要创建一个头文件并根据需要包含它。

If you move all your code to "newFile.m", create "newFile.h" and put all your method signatures in the header file. Then in your old file, do "#include oldFile.h".

如果将所有代码移动到“newFile.m”,请创建“newFile.h”并将所有方法签名放在头文件中。然后在旧文件中,执行“#include oldFile.h”。

#1


You'll need to split your methods in to different "categories," which each get their own h and m files. You'll also need an h and m file for the class itself. Here's a simple little example that I think will show you what you need to do.

您需要将方法拆分为不同的“类别”,每个类别都有自己的h和m文件。你还需要一个h和m文件来为类本身。这是一个简单的小例子,我认为它会告诉你你需要做什么。

TestClass.h

#import <Cocoa/Cocoa.h>
@interface TestClass : NSObject {
}
@end

TestClass.m

#import "TestClass.h"
@implementation TestClass
@end

TestClass+Category1.h

#import <Cocoa/Cocoa.h>
#import "TestClass.h"
@interface TestClass(Category1) 
-(void)TestMethod1;
@end

TestClass+Category1.m

#import "TestClass+Category1.h"
@implementation TestClass(Category1)
-(void)TestMethod1 {
NSLog(@"This is the output from TestMethod1");
}
@end

TestClass+Category2.h

#import <Cocoa/Cocoa.h>
#import "TestClass.h"
@interface TestClass(Category2) 
-(void)TestMethod2;
@end

TestClass+Category2.m

#import "TestClass+Category2.h"
@implementation TestClass(Category2)
-(void)TestMethod2 {
NSLog(@"This is the output from TestMethod2");
}
@end

Then in whatever file is using your class, you'll use

然后在任何使用你的课程的文件中,你将使用

#import "TestClass.h"
#import "TestClass+Category1.h"
#import "TestClass+Category2.h"

Now you can create an instance of class TestClass and it will have all the methods from both category1 and category2. Simply use

现在,您可以创建类TestClass的实例,它将包含category1和category2中的所有方法。简单地使用

TestClass* test = [[TestClass alloc] init];
[test TestMethod1];
[test TestMethod2];

#2


You need to create a header file and include it as appropriate.

您需要创建一个头文件并根据需要包含它。

If you move all your code to "newFile.m", create "newFile.h" and put all your method signatures in the header file. Then in your old file, do "#include oldFile.h".

如果将所有代码移动到“newFile.m”,请创建“newFile.h”并将所有方法签名放在头文件中。然后在旧文件中,执行“#include oldFile.h”。