如何在 Objective-C 中设置非实例化类(仅包含方法的类)

发布于 2024-11-08 21:33:01 字数 174 浏览 0 评论 0原文

我正在寻找在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

人疚 2024-11-15 21:33:01

这在 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)

浊酒尽余欢 2024-11-15 21:33:01

你想创建类方法吗?

@interface Foo : NSObject {}
+(NSMutableArray*)someClassMethod:(id)params;
@end

...

@implementation Foo
+(NSMutableArray*)someClassMethod:(id)params {
   // whatever implementation 
   return nil;
}
@end

...

NSMutableArray* array = [Foo someClassMethod:nil];

You want to make class methods?

@interface Foo : NSObject {}
+(NSMutableArray*)someClassMethod:(id)params;
@end

...

@implementation Foo
+(NSMutableArray*)someClassMethod:(id)params {
   // whatever implementation 
   return nil;
}
@end

...

NSMutableArray* array = [Foo someClassMethod:nil];
余生再见 2024-11-15 21:33:01

如果您只执行函数,并且不需要支持子类化等,为什么不将它们编写为 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?

老子叫无熙 2024-11-15 21:33:01

如果这只是一个执行某些功能的类,您可以将其编写为 C 函数。

在你的头文件中——

NSMutableArray *functionThatReturnsMutableArray(NSObject *param1, NSString *param2);

在你的实现文件中——

NSMutableArray *functionThatReturnsMutableArray(NSObject *param1, NSString *param2)
{
   ...
   return aMutableArray;
}

只需在需要这些函数的类中包含 .h 文件并直接调用它们即可。

NSMutableArray *anArray = functionThatReturnsMutableArray(param1, param2);

If this is just a class that performs some functions, you could write it as a C function.

In your header file --

NSMutableArray *functionThatReturnsMutableArray(NSObject *param1, NSString *param2);

In your implementation file --

NSMutableArray *functionThatReturnsMutableArray(NSObject *param1, NSString *param2)
{
   ...
   return aMutableArray;
}

And that just include the .h file in your class that needs these functions and call them directly.

NSMutableArray *anArray = functionThatReturnsMutableArray(param1, param2);
眼眸印温柔 2024-11-15 21:33:01

根据您正在做的事情(相同的 NSString 操作、UIView 操作等),您可以实现一个类别(我回答了 昨天的一个问题,解释如下 - 为方便起见复制;)。

类别使用附加方法或现有方法的您的版本扩展现有类。例如,假设您想要添加一个将字符串的第一个字母返回到 NSString 的方法。为此,您将创建一个类别,如下所示:

接口 - JULString.h

#import NSString

@interface NSString (JULString)

-(NSString *) firstLetter;

@end

实现 - 典型的约定是类别的文件名是您要扩展的类的名称,后跟“+”和类别的名称。在这种情况下,该文件将被称为 NSString+JULString.m

#import "NSString+JULString.h"

@implementation NSString ( JULString )

- (NSString *)firstLetter
{
  return [NSString stringWithFormat:@"%C", [self characterAtIndex:1]];
}
@end

类别的巧妙之处在于,现在它们扩展了您正在使用的类的任何实例的行为。换句话说,应用程序中的任何 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

#import NSString

@interface NSString (JULString)

-(NSString *) firstLetter;

@end

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

#import "NSString+JULString.h"

@implementation NSString ( JULString )

- (NSString *)firstLetter
{
  return [NSString stringWithFormat:@"%C", [self characterAtIndex:1]];
}
@end

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 ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文