我们可以使用 NSMutable 对象作为非 NSMutable 类的成员吗

发布于 2024-11-05 06:21:04 字数 213 浏览 0 评论 0原文

假设我们有一个简单的 NSDictionary 类,它的对象之一可以是 NSMutableDictionary 对象吗?当我们编辑 NSMutableDictionary 对象中的值时,我们只是编辑 NSDictionary 对象的值。既然我们没有编辑 NSDictionary 的对象,那么对于非可变的 NSDictionary 类来说,这应该是一个问题吗?

Suppose we have simple NSDictionary class, can one of its objects be an NSMutableDictionary object? When we edit a value within the NSMutableDictionary object, we are only editing values of the object of NSDictionary. Since we are not editing the object of NSDictionary, should it be a problem for the non-mutable NSDictionary class?

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

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

发布评论

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

评论(1

野鹿林 2024-11-12 06:21:04

集合类的可变性仅指修改集合作为一个整体的能力,而不是成员的能力。事实上,集合只是由指向它所包含的对象的指针组成;他们的一切都没有改变。将对象放入不可变集合中不会改变对象本身的修改能力。

所以,是的,您可以毫无困难地修改 NSDictionary 内的 NSMutableDictionary

NSDictionary * myDict;
myDict = [NSDictionary dictionaryWithObject:
            [NSMutableDictionary dictionaryWithObject:@"This is one string"
                                               forKey:@"sampleKey"]
                                     forKey:@"mutableDict"];

NSMutableDictionary * myMutableDict = [myDict objectForKey:@"mutableDict"];

NSLog(@"%@", [myMutableDict objectForKey:@"sampleKey"];
// Prints "This is one string"

[[myDict objectForKey:@"mutableDict"] setObject:@"Not the same as before" 
                                         forKey:@"sampleKey"];

NSLog(@"%@", [myMutableDict objectForKey:@"sampleKey"];    
// Prints "Not the same as before"

对于任何不可变集合中包含的任何对象(允许修改)也是如此:

@interface MyNeatObjectClass : NSObject {
        NSString * neatString;
}

- (id)initWithNeatString:(NSString *)initialString;
- (void)setNeatString:(NSString *)newString;
- (NSString *)neatString;

MyNeatObjectClass * myObj = [[MyNeatObjectClass alloc] 
                                         initWithNeatString:@"Example string"];

NSLog(@"%@", [myObj neatString]);    // Prints "Example string"
NSArray * arr = [NSArray arrayWithObject:myObj];
[myObj release];

// instance of MyNeatObjectClass
[[arr objectAtIndex:0] setNeatString:@"Another string"];
NSLog(@"%@", [[arr objectAtIndex:0] neatString]);     // Prints "Another string"

The mutability of a collection class refers only to the ability to modify the collection as a whole, not the members. The collection in fact just consists of pointers to the objects it contains; nothing about them is changed. Putting an object inside an immutable collection does not change the object's own ability to be modified.

So, yes, you can modify an NSMutableDictionary inside an NSDictionary, without difficulty.

NSDictionary * myDict;
myDict = [NSDictionary dictionaryWithObject:
            [NSMutableDictionary dictionaryWithObject:@"This is one string"
                                               forKey:@"sampleKey"]
                                     forKey:@"mutableDict"];

NSMutableDictionary * myMutableDict = [myDict objectForKey:@"mutableDict"];

NSLog(@"%@", [myMutableDict objectForKey:@"sampleKey"];
// Prints "This is one string"

[[myDict objectForKey:@"mutableDict"] setObject:@"Not the same as before" 
                                         forKey:@"sampleKey"];

NSLog(@"%@", [myMutableDict objectForKey:@"sampleKey"];    
// Prints "Not the same as before"

Likewise for any object (which allows modification) contained inside any immutable collection:

@interface MyNeatObjectClass : NSObject {
        NSString * neatString;
}

- (id)initWithNeatString:(NSString *)initialString;
- (void)setNeatString:(NSString *)newString;
- (NSString *)neatString;

MyNeatObjectClass * myObj = [[MyNeatObjectClass alloc] 
                                         initWithNeatString:@"Example string"];

NSLog(@"%@", [myObj neatString]);    // Prints "Example string"
NSArray * arr = [NSArray arrayWithObject:myObj];
[myObj release];

// instance of MyNeatObjectClass
[[arr objectAtIndex:0] setNeatString:@"Another string"];
NSLog(@"%@", [[arr objectAtIndex:0] neatString]);     // Prints "Another string"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文