可变对象与不可变对象(目标 C 中的实例变量)
我有一个带有属性 sampleNames.
的类。它的类型为 NSSet。我计划使用的实例变量是 NSMutableSet。
我使用 NSMutableSet 的唯一原因是为了自己方便。但我记得有人“告诉”可变类应尽可能少用,原因我不记得了。我了解如何使用这两种方式,所以这不是一个问题。
在决定是否在内部使用可变类时,有哪些实际考虑因素?
请注意,我对将 PROPERTY 更改为 NSMutableSet 一点也不感兴趣。但我在内部使用不同类型的事实让我思考为什么,除了方便之外我没有真正的理由,我发现这本身就是一个糟糕的理由。也许这是唯一的原因,但我并没有主动意识到我为了方便而交易的是什么。)
I have a class with a property sampleNames.
It is of type NSSet. The instance variable I plan to use will be NSMutableSet.
the only reason I have for using NSMutableSet is convenience for myself. But I recall being 'told' that Mutable class should be used as sparingly as possible for some reason I cannot remember. I understand how to use both ways, so this si not so much a question about that.
What are the practical considerations when deciding whether or not to use a mutable class internally?
Please note, I am not at all interested in changing my PROPERTY to NSMutableSet. But the fact that I am using a different type internally made me think about why and I have no real justification other than convenience, Which I have found to be a bad justification by itself. Maybe it is the only reason, but i am not actively aware of what I am trading for the convenience.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你很困惑。
当您需要更改集合的内容时,可以使用 NSMutableSet。只有当它是可变的时你才能这样做。
如果您使用 NSSet,则无法更改内容。
所以...您的属性声明和实例变量应该是同一个类。如果不需要更改内容,请使用 NSSet;如果需要,请使用 NSMutableSet。
为属性和实例变量使用不同的类会带来灾难......这不方便而且令人困惑=不好;-)
I think you are confused.
You use NSMutableSet when you need to change the contents of the set. You can only do that if it is mutable.
If you use NSSet, then you can't change the contents.
So ... your property declaration and your instance variable should be the SAME class. Either use NSSet if you don't need to change the contents or NSMutableSet if you do.
Having a different class for the property and the instance variable is a recipe for disaster ... it's not convenient and it is confusing = BAD ;-)