假设我有一个Objective-c .m文件,定义了以下方法:
- (void) doOneThing {
[self doAnotherThing];
}
- (void) doAnotherThing {
[self stillOtherThings];
}
Run Code Online (Sandbox Code Playgroud)
如果我编译它,xcode会向我发出警告,该类可能不响应-doAnotherThings,因为doAnotherThing定义在-doOneThing下面,并且编译器在编译-doOneThing时还不知道-doAnotherThing.当然,代码编译正确并且确实有效,但我想摆脱那条警告信息.
解决这个问题的简单方法是在-doOneThing之前定义-doAnotherThing,但有时我喜欢在源代码中对相关方法进行分组,使其难以重新排序.如果这是C,我可以这样做:
void doAnotherThing();
void doOneThing() {
doAnotherThing();
}
void doAnotherThing() {
...still other things...
}
Run Code Online (Sandbox Code Playgroud)
将定义与声明分开.有没有办法在objective-c中做这样的事情,或以其他方式解决我的问题?
解决这个问题的典型方法如下:
//in DoThings.h
@interface DoThings : NSObject {
//instance variables go here
}
//public methods go here
- (void) doAPublicThing;
//properties go here
@end
//in DoThings.m
@interface DoThings (Private)
- (void)doOneThing;
- (void)doAnotherThing;
- (void)stillOtherThings;
@end
@implementation DoThings
- (void) doAPublicThing {
[self doOneThing];
}
- (void) doOneThing {
[self doAnotherThing];
}
- (void) doAnotherThing {
[self stillOtherThings];
}
@end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
131 次 |
| 最近记录: |