如何在 Objective-C 中设置非实例化类(仅包含方法的类)
我正在寻找在 Objective-C 中为专注于获取数据的 iOS 项目创建一个类。我熟悉类的正常工作方式、setter 和 getter 方法以及变量。然而,对于这个类,因为它只执行一个函数(返回 NSMutableArrays),所以我不想创建该类的实例来使用类内的方法。
知道我如何才能整齐而有效地做到这一点吗?
I'm looking to create a class in Objective-C for an iOS project that is focused on fetching data. I'm familiar with how classes normally work, setter and getter methods and variables. However, for this class since it's only performing a function (returning NSMutableArrays) I don't want to have to create an instance of the class to use the methods inside the class.
Any idea how I can do this neatly and efficiently?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这在 Objective-C 中有点不典型。由于 Objective-C 中的类实际上无法拥有普通函数可用的状态(即没有类变量),因此从未实例化的类在大多数情况下相对无用。这种功能的正常设计模式是:
单例类(如果您需要大量状态)
一组函数(如果您 需要大量状态)不要)
This is a little bit atypical in Objective-C. Since classes in Objective-C can't actually have state beyond what is available to ordinary functions (i.e. there are no class variables), a class that's never instantiated is relatively useless in most cases. The normal design patterns for this kind of functionality are:
A singleton class (if you need lots of state)
A set of functions (if you don't)
你想创建类方法吗?
You want to make class methods?
如果您只执行函数,并且不需要支持子类化等,为什么不将它们编写为 C 函数而不是带有方法的类呢?
If you're only performing functions, and you don't need to support subclassing etc, why not just write them as C functions rather than a class with methods?
如果这只是一个执行某些功能的类,您可以将其编写为 C 函数。
在你的头文件中——
在你的实现文件中——
只需在需要这些函数的类中包含 .h 文件并直接调用它们即可。
If this is just a class that performs some functions, you could write it as a C function.
In your header file --
In your implementation file --
And that just include the .h file in your class that needs these functions and call them directly.
根据您正在做的事情(相同的 NSString 操作、UIView 操作等),您可以实现一个类别(我回答了 昨天的一个问题,解释如下 - 为方便起见复制;)。
类别使用附加方法或现有方法的您的版本来扩展现有类。例如,假设您想要添加一个将字符串的第一个字母返回到 NSString 的方法。为此,您将创建一个类别,如下所示:
接口 - JULString.h
实现 - 典型的约定是类别的文件名是您要扩展的类的名称,后跟“+”和类别的名称。在这种情况下,该文件将被称为 NSString+JULString.m
类别的巧妙之处在于,现在它们扩展了您正在使用的类的任何实例的行为。换句话说,应用程序中的任何 NSString 都将具有您的新方法(当然前提是您导入了正确的头文件)。但请注意,权力越大,责任越大。使用类别行为覆盖类可能会导致不良效果,所以要小心。
您可能需要检查的几个链接是:
注意:
我没有带 Mac,所以我基本上是凭空编写这段代码(并使用上面网站上的一些代码作为提醒)。因此,对于任何错误,我提前表示歉意;)
Depending on what you are doing (the same NSString operations, UIView manipulations, etc), you could implement a category (I answered a question yesterday with the explanation below -- copied for your convenience ;).
Categories extend an existing class with additional methods or with your version of existing methods. For example, let's say you want to add a method that returns the first letter of a string to NSString. To do this you would create a category as follows:
Interface - JULString.h
Implementation - The typical convention is that the filename of the category is the name of the class you are extending followed by “+” and the name of the category. In this case the file would be called NSString+JULString.m
The neat thing about categories is that now they extend the behavior of ANY instance of the class you are working with. In other words, any NSString in your application will have your new methods (provided that you import the proper header file of course). Beware though, as with great power comes great responsibility. Overwriting class using a category behaviors may lead to undesired effects, so be cautious.
A couple of links you may want to check are:
Note:
I don't have my Mac with me so I'm writing this code basically off the top of my head (and using some code from the sites above as a reminder). So I apologize in advance for any mistakes ;)