Objective-C:如何向类外隐藏类成员?

发布于 2024-10-09 10:21:10 字数 772 浏览 3 评论 0原文

我正在与某些事情作斗争,但找不到任何令人满意的解决方案。

我有一个带有“myMutableArray”成员的类。

我希望该类能够自行管理从数组中添加和删除项目,因此我不希望任何其他类能够访问该成员并调用其上的 NSMutableArray 方法。

在理想情况下,我希望该成员有一个私有 getter(以便能够调用 self.myMutableArray)和一个公共 setter。

你知道我如何实现这一目标吗?

换句话说:

我希望其他类

能够调用

- [oneInstance setMyMutableArray:thisArray]; // set
- oneInstance.myMutableArray = thisArray; // set using setter

- thisArray = oneInstance.myMutableArray; // get
- [oneInstance addItem:anItem]; // add

无法调用:

- [oneInstance.myMutableArray add:etc...] // add

我希望我的类

能够调用

- self.myMytableArray = [NSMutableArray array]; // set
- thisArray = self.myMytableArray ; // get

谢谢。

I'm fighting with something and I don't find any satisfying solution.

I have a class with a "myMutableArray" member.

I would like the class to manage itself adding and removing items from the array, so I don't want any other class being able to access the member and call NSMutableArray methods on it.

In an ideal situation, I would like to have a private getter (to be able to call self.myMutableArray) and a public setter for this member.

Do you know how I may achieve this ?

In other words :

I would like other classes

be able to call

- [oneInstance setMyMutableArray:thisArray]; // set
- oneInstance.myMutableArray = thisArray; // set using setter

- thisArray = oneInstance.myMutableArray; // get
- [oneInstance addItem:anItem]; // add

not being able to call :

- [oneInstance.myMutableArray add:etc...] // add

I would like my class

be able to call

- self.myMytableArray = [NSMutableArray array]; // set
- thisArray = self.myMytableArray ; // get

Thank you.

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

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

发布评论

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

评论(2

等风来 2024-10-16 10:21:10

您有什么理由需要公共设置器吗?听起来好像类本身拥有该数组。您可能最好不要提供对该字段的任何公共属性访问权限,并创建一个将值复制到私有字段中的公共方法。

// public interface, in the .h file
@interface MyClass : // superclass, protocols, etc.
- (void) setSomething:(NSArray *)values;
@end

// private interface, not in the .h
@interface MyClass ()
@property (/* attributes */) NSMutableArray *myMutableArray;
@end

@implementation MyClass
@synthesize myMutableArray = myMutableArray_;

- (void) setSomething:(NSArray *)values
{
    [self.myMutableArray setArray:values];
}

@end

Is there any reason you need the public setter? It sounds like the class itself owns the array. You'd probably be better off not providing any public property access to the field, and making a public method which copies the values into your private field.

// public interface, in the .h file
@interface MyClass : // superclass, protocols, etc.
- (void) setSomething:(NSArray *)values;
@end

// private interface, not in the .h
@interface MyClass ()
@property (/* attributes */) NSMutableArray *myMutableArray;
@end

@implementation MyClass
@synthesize myMutableArray = myMutableArray_;

- (void) setSomething:(NSArray *)values
{
    [self.myMutableArray setArray:values];
}

@end

Foo.h

@interface Foo : NSObject
@property(readonly, retain) NSArray * myReadonlyArray;
- (void) addItem: (Item *) anItem;
- (BOOL) publiclyDoSomething;
@end

Foo.m

@interface Foo()
@property(readwrite, retain) NSMutableArray * myMutableArray;
- (void) doSomethingInPrivate;
@end

@implementation Foo
@synthesize myMutableArray = myMutableArray_;

- (void) addItem: (Item *) anItem
{
    // assuming myMutableArray_ was already iniitialized
    [self.myMutableArray addObject: anItem];
}

- (NSArray *)myReadonlyArray
{
     return self.myMutableArray;
}

... rest of methods (including the public/private) implementations ...
@end

一些细节:

  • Objective-C 有“实例变量”,而不是“成员变量”。

  • 上面定义了自动合成的公共 getter 和私有 setter。为了清楚起见,我还添加了一个公共方法和一个私有方法。

  • Objective-C 中的“公共”和“私有”完全由编译器的可见性定义。 myMutableArray 的 setter 和方法 doSomethingInPrivate 只是私有的,因为它们在 @interface 中的声明无法导入。

  • self.myMutableArray[self myMutableArray] 做同样的事情; . 语法仅仅是等效方法调用的简写(除了这个问题之外还有一些边缘情况细节)

  • @interface< 中的

    @property /code> 纯粹是方法声明的简写(带有一些额外的元数据)。

  • @interface Foo() 是一个类扩展,而不是类别。它的存在正是出于上述目的;使用范围应受到限制的附加声明信息来扩展类的@interface。它可以出现在头文件中,也就是说,您只需导入库的实现即可创建库私有功能。

  • 当您既不提供 @synthesize@property 也不提供常规方法实现时,则使用

    @dynamic否则不需要!

我可能忘记了什么。

Foo.h

@interface Foo : NSObject
@property(readonly, retain) NSArray * myReadonlyArray;
- (void) addItem: (Item *) anItem;
- (BOOL) publiclyDoSomething;
@end

Foo.m

@interface Foo()
@property(readwrite, retain) NSMutableArray * myMutableArray;
- (void) doSomethingInPrivate;
@end

@implementation Foo
@synthesize myMutableArray = myMutableArray_;

- (void) addItem: (Item *) anItem
{
    // assuming myMutableArray_ was already iniitialized
    [self.myMutableArray addObject: anItem];
}

- (NSArray *)myReadonlyArray
{
     return self.myMutableArray;
}

... rest of methods (including the public/private) implementations ...
@end

Some details:

  • Objective-C has "instance variables", not "member variables".

  • The above defines a public getter and private setter that is synthesized automatically. For clarity's sake, I also added a public method and a private method.

  • "Public" and "private" in Objective-C are defined entirely by visibility to the compiler. The setter for myMutableArray and the method doSomethingInPrivate are only private because their declarations in an @interface cannot be imported.

  • self.myMutableArray and [self myMutableArray] do the same thing; the . syntax is merely short hand for an equivalent method call (with a few edge case details beyond this question)

  • @property in the @interface is purely short hand for method declarations (with a bit of extra metadata).

  • @interface Foo() is a class extension and not a category. It exists for exactly the purpose demonstrated above; to extend the @interface of a class with additional declarative information whose scope should be limited. It can appear in a header file that, say, you only import in your library's implementation to create library-private functionality.

  • @dynamic is used when you neither @synthesize an @property nor provide a conventional method implementation. It is not needed otherwise!

I'm probably forgetting something.

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