xcode 我将在 Objective C 中的哪个位置放置全局效用函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14078702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:25:01  来源:igfitidea点击:

Where would I place a global utility function in Objective C?

objective-ciosxcodecocoa

提问by Ryan

When writing an iOS app, where would I place a function that I intend to use from any other file?

在编写 iOS 应用程序时,我应该在哪里放置我打算从任何其他文件使用的函数?

For example, a function to convert a NSDate to a relative time string ("5 secs ago"). Would I make a class and make these functions all static?

例如,将 NSDate 转换为相对时间字符串(“5 秒前”)的函数。我会创建一个类并使这些功能全部静态吗?

采纳答案by jlehr

Functions can be placed wherever convenient. If a function or group of functions is likely to be imported in many places, you can declare and implement them in their own .h/.m pair. So for example you might implement your date conversion function in a file named XYZDateUtilities.m, and declare it in XYZDateUtilities.h.

函数可以放在任何方便的地方。如果一个函数或一组函数可能在很多地方被导入,你可以在它们自己的 .h/.m 对中声明和实现它们。例如,您可以在名为XYZDateUtilities.m的文件中实现日期转换函数,并在XYZDateUtilities.h 中声明它。

Declaring functions with the staticqualifier would limit their scope to the file in which they were declared, so you wouldn't want to do that; in fact you'd want to do the opposite -- declare them as externin the .h file so that they'll be visible in other files.

使用static限定符声明函数会将它们的范围限制在声明它们的文件中,因此您不会想要这样做;事实上,你想要做相反的事情——将它们声明为extern.h 文件,以便它们在其他文件中可见。

回答by JRG-Developer

You have a couple options:

你有几个选择:

1) If you're extending the behavior of a class (such as the NSDate string conversion method you described), it may work best to simply create a category on said class.

1) 如果您要扩展类的行为(例如您描述的 NSDate 字符串转换方法),最好在该类上简单地创建一个类别。

Here's a tutorial on iOS categories:

这是有关 iOS 类别的教程:

http://mobile.tutsplus.com/tutorials/iphone/objective-c-categories/

http://mobile.tutsplus.com/tutorials/iphone/objective-c-categories/

Important Note:

重要的提示:

Categories change a class's behavior (if you override a method) everywhere within the project whether or not you include the header (.h) file in another specific class's imports

无论您是否在另一个特定类的导入中包含头 (.h) 文件,类别都会在项目中的任何地方更改类的行为(如果您覆盖方法)

For this reason, it's generally best to notoverride methods via a category, but instead, to create a subclass if you want to change certain methods.

出于这个原因,通常最好不要通过类别覆盖方法,而是如果要更改某些方法,则创建子类。

For adding new methods, however, categories can be very convenient and useful.

然而,对于添加新方法,类别可能非常方便和有用。

2) If you want to create a new class that's imported everywhere, you can create said class and put its header import, i.e. #import "MyClass.h", into your project's prefix.pchfile (found under the "supporting files" group within the project by default).

2)如果你想创建一个在任何地方都导入的新类,你可以创建该类并将其头文件导入,即#import "MyClass.h",放入你的项目prefix.pch文件(默认在项目内的“支持文件”组下)。

Anything that you put into the prefix.pchfile will be available anywhere within your app. This is also a useful place to put constants (such as strings) or define enums that are used across many classes within the app.

您放入prefix.pch文件中的任何内容都可以在您的应用程序中的任何位置使用。这也是放置常量(例如字符串)或定义在应用程序内的许多类中使用的枚举的有用位置。

I hope this helps. Let me know if further clarification is needed, and I'll do my best to help.

我希望这有帮助。如果需要进一步说明,请告诉我,我会尽力提供帮助。

Cheers!

干杯!

回答by Jog

Another option would be to create a class for your helper methods and implement all the helpers as class methods.

另一种选择是为您的助手方法创建一个类,并将所有助手实现为类方法。

e.g. HelperClass.h

例如HelperClass.h

+ (NSString *)getFrenchCapital

e.g. HelperClass.m

例如HelperClass.m

+ (NSString *)getFrenchCapital
{
    return @"Paris";
}

Then import your helper class wherever you need it, and simply call the class methods:

然后在任何需要的地方导入您的助手类,只需调用类方法:

e.g. Foo.m

例如Foo.m

#import "HelperClass.h"

...

- (void)logFrenchCapital
{
    NSLog(@"Capital of France: %@", [HelperClass getFrenchCapital]);
} 

回答by TieDad

If you make all functions static in a class, then alternative is to just define functions in .m file, and extern functions in .h file, just like what you do in C.

如果您将类中的所有函数设为静态,则替代方法是在 .m 文件中定义函数,在 .h 文件中定义外部函数,就像您在 C 中所做的一样。