如何声明由可变类型支持的不可变属性?

发布于 2024-12-09 09:42:41 字数 498 浏览 0 评论 0原文

我想声明一个公共不可变属性:

@interface Foo
@property(strong, readonly) NSSet *items;
@end

……在实现文件中以可变类型为后盾:

@interface Foo (/* private interface*/)
@property(strong) NSMutableSet *items;
@end

@implementation
@synthesize items;
@end

我想要的是实现中的一个可变集合,当从外部访问时,该集合会被转换为不可变集合。 (我不在乎调用者可以将实例强制转换回 NSMutableSet 并破坏封装。我住在一个安静、体面的小镇,这样的事情不会发生。)

现在我的编译器将实现中的属性为 NSSet。我知道有很多方法可以让它工作,例如使用自定义 getter,但是有没有一种方法可以简单地使用声明的属性来完成它?

I’d like to declare a public immutable property:

@interface Foo
@property(strong, readonly) NSSet *items;
@end

…backed with a mutable type in the implementation file:

@interface Foo (/* private interface*/)
@property(strong) NSMutableSet *items;
@end

@implementation
@synthesize items;
@end

What I want is a mutable collection in the implementation that gets cast into an immutable one when accessed from the outside. (I don’t care that the caller can cast the instance back to NSMutableSet and break the encapsulation. I live in a quiet, decent town where such things don’t happen.)

Right now my compiler treats the property as NSSet inside the implementation. I know there are many ways to get it working, for example with custom getters, but is there a way to do it simply with declared properties?

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

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

发布评论

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

评论(3

找回味觉 2024-12-16 09:42:41

最简单的解决方案就是

@interface Foo {
@private
    NSMutableSet* _items;
}

@property (readonly) NSSet* items;

@synthesize items = _items;

您的实现文件中。然后您可以通过 _items 访问可变集,但公共接口将是一个不可变集。

The easiest solution is

@interface Foo {
@private
    NSMutableSet* _items;
}

@property (readonly) NSSet* items;

and then just

@synthesize items = _items;

inside your implementation file. Then you can access the mutable set through _items but the public interface will be an immutable set.

有深☉意 2024-12-16 09:42:41

您必须将公共接口中的属性声明为只读,以便编译器不会抱怨。像这样:

Word.h

#import <Foundation/Foundation.h>

@interface Word : NSObject
@property (nonatomic, strong, readonly) NSArray *tags;
@end

Word.m

#import "Word.h"

@interface Word()
@property (nonatomic, strong) NSMutableArray *tags;
@end

@implementation Word
@end

You have to declare the property in the public interface as readonly in order the compiler not to complain. Like this:

Word.h

#import <Foundation/Foundation.h>

@interface Word : NSObject
@property (nonatomic, strong, readonly) NSArray *tags;
@end

Word.m

#import "Word.h"

@interface Word()
@property (nonatomic, strong) NSMutableArray *tags;
@end

@implementation Word
@end
千寻… 2024-12-16 09:42:41

您可以在实现中自己完成:

@interface Foo : NSObject
    @property(nonatomic, retain) NSArray *anArray;
@end

--- 实现文件 ---

@interface Foo()
{
    NSMutableArray *_anArray;
}
@end

@implementation Foo

- (NSArray *)anArray
{
    return _anArray;
}

- (void)setAnArray:(NSArray *)inArray
{
     if ( _anArray == inArray )
     {
         return;
     }
     [_anArray release];
     _anArray = [inArray retain];
}

- (NSMutableArray *)mutablePrivateAnArray
{
    return _anArray;
}

@end

You could just do it yourself in your implementation:

@interface Foo : NSObject
    @property(nonatomic, retain) NSArray *anArray;
@end

--- implementation file ---

@interface Foo()
{
    NSMutableArray *_anArray;
}
@end

@implementation Foo

- (NSArray *)anArray
{
    return _anArray;
}

- (void)setAnArray:(NSArray *)inArray
{
     if ( _anArray == inArray )
     {
         return;
     }
     [_anArray release];
     _anArray = [inArray retain];
}

- (NSMutableArray *)mutablePrivateAnArray
{
    return _anArray;
}

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