“额外”应该放在哪里?执行?
有时,我会看到为对象创建新方法的代码片段,如下所示:
@implementation UIImage (Extras)
- (void)aMethod:(id)anObject {
// some functionality
}
@end
我将该代码放在哪里?我是否将其放在我当前正在为其编写代码的类中?如果是这样,我需要将其放在代码中的什么位置?
谢谢。
Occationaly I see snippets of code creating new methods for objects and such that looks like this:
@implementation UIImage (Extras)
- (void)aMethod:(id)anObject {
// some functionality
}
@end
Where do I put this code? Do I put it in the class I'm currently writing code for? If so at what point in the code do I need to put this?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将此类别代码放在您喜欢的任何位置。一般来说,此代码应位于名为 UIImage+Extras.m 的文件和匹配的头文件 UIImage+Extras.h 中。
You can put this category code whereever you like. In general, this code should be in a file called UIImage+Extras.m and a matching header file UIImage+Extras.h.
这是 Objective-C 的一项功能,称为“类别”。有关详细信息,请参阅以下文章:
This is an Objective-C feature known as a "category". See these articles for more info:
为了简单起见并保持代码整洁,我通常将类类别放在单独的文件中。
但总的来说,我认为您只需要在某个标头中声明您的类别并导入它即可让编译器知道您添加的方法。这些方法的实现可以放在任何(实现)文件中,但我再次认为最好将其保存在单独的位置。
For the sake of simplicity and to keep code clean I usually put class categories in separate files.
But in general I think you just need to declare your category in some header and import it to let compiler know about methods you add. Implementation of those methods can be put in any (implementation) file, but once again I think it is better to keep it in separate place.