Objective-C –在类中执行 NSMutableArray 操作但返回 NSArray

发布于 2024-12-18 01:56:08 字数 272 浏览 4 评论 0原文

我很抱歉这个有点令人困惑的标题,但我有一个类,其接口如下所示:

@interface Class : NSObject {

    NSArray *items;
}

@property (nonatomic, retain) NSArray *items;

@end

在实现中,我实际上想做一些添加操作,因此使用 NSMutableArray ,然后用户使用类应该能够以 NSArray 形式检索数据。

I'm sorry for the little confusing title but say I have a class with an interface that looks like this:

@interface Class : NSObject {

    NSArray *items;
}

@property (nonatomic, retain) NSArray *items;

@end

In the impementation I actually want to do some adding operations thus using NSMutableArray and then the user using the class should be able to retrieve the data as an NSArray.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

ζ澈沫 2024-12-25 01:56:08

将可变数组声明为实例变量,将属性声明为只读 NSArray 并覆盖 getter。

@interface SomeClass : NSObject {

    NSMutableArray *mutableItems;
}

@property (nonatomic, readonly) NSArray *items;

@end

@implementation SomeClass

- (NSArray *)items {
    return [NSArray arrayWithArray:mutableItems];
}

@end

Declare mutable array as instance variable, declare the property as readonly NSArray and overwrite the getter.

@interface SomeClass : NSObject {

    NSMutableArray *mutableItems;
}

@property (nonatomic, readonly) NSArray *items;

@end

@implementation SomeClass

- (NSArray *)items {
    return [NSArray arrayWithArray:mutableItems];
}

@end
那伤。 2024-12-25 01:56:08

没有什么理由不从声明为返回 NSArray 的方法中返回 NSMutableArray。

There is little reason not to just return an NSMutableArray from a method declared as returning an NSArray.

迷你仙 2024-12-25 01:56:08

如果您不希望用户更改数组,您可以这样做:

@interface Class : NSObject 
- (NSArray*) items;
@end

@implementation Class
{
   NSMutableArray *items;
}

// Allocate 'items' somewhere in your init method

- (NSArray*)items
{
  return items;
}

@end

Well if you don't want the user to change the array you can just do it like this:

@interface Class : NSObject 
- (NSArray*) items;
@end

@implementation Class
{
   NSMutableArray *items;
}

// Allocate 'items' somewhere in your init method

- (NSArray*)items
{
  return items;
}

@end
梦幻的味道 2024-12-25 01:56:08

您可以简单地将变量定义为您想要的类型,而不是为实例变量定义属性:

@interface Class : NSObject {
    NSMutableArray *items;
}

并将其用作 NSMutableArray

- (id)init {
    if ((self == [super init])) {
        items = [[NSMutableArray alloc] init]; // Don't forget to release me!
    } 
}

但是然后在您的界面中,定义一个 getter 方法返回一个 NSArray:

- (NSArray *)items;

并按如下方式实现它:

- (NSArray *)items {
    return [NSArray arrayWithArray:items];
}

Instead of defining a property for your instance variable, you could simply define the variable as the type you want it to be:

@interface Class : NSObject {
    NSMutableArray *items;
}

And use it as an NSMutableArray:

- (id)init {
    if ((self == [super init])) {
        items = [[NSMutableArray alloc] init]; // Don't forget to release me!
    } 
}

But then in your interface, define a getter method that returns an NSArray:

- (NSArray *)items;

And implement it as such:

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