From the docs , 在集合对象中存储可变对象可能会导致问题。如果某些集合包含的对象发生变异,则某些集合可能会变得无效甚至损坏,因为通过变异,这些对象会影响它们在集合中的放置方式。首先,作为散列集合中键的对象的属性,例如 NSDictionary 对象或 NSSet 对象,如果更改,如果更改的属性影响对象的散列或 isEqual: 方法的结果,则会破坏集合。 (如果集合中对象的哈希方法不依赖于它们的内部状态,那么损坏的可能性就较小。)其次,如果有序集合中的对象(例如排序数组)的属性发生了变化,这可能会影响对象如何与数组中的其他对象进行比较,从而导致排序无效。
但是当我记录结果时,该对象被完美地添加到集合中。其实这样做不安全??
-(void)mutableObjectsinCollections{
NSMutableArray *arr1 = [[NSMutableArray alloc]init];
NSMutableArray *arr2 = [[NSMutableArray alloc]init];
NSMutableArray *arr3 = [[NSMutableArray alloc]init];
NSMutableArray *arr4 = [[NSMutableArray alloc]init];
[self addObjectsToArrayBefore:arr1];
[self addObjectsToArrayBefore:arr2];
[self addObjectsToArrayBefore:arr3];
[self addObjectsToArrayBefore:arr4];
NSDictionary *dict = @{@"arr1": arr1,@"arr2": arr2,@"arr3": arr3,@"arr4": arr4,@"arr5": arr5};
NSLog(@"%@",dict);
[self addObjectsToArrayAfter:arr1];
[self addObjectsToArrayAfter:arr2];
[self addObjectsToArrayAfter:arr3];
[self addObjectsToArrayAfter:arr4];
NSLog(@"%@",dict);
}
-(void)addObjectsToArrayBeforeNSMutableArray *)arr{
for (int i = 1; i<10; i++) {
[arr addObject:[NSNumber numberWithInteger:i]];
}
}
-(void)addObjectsToArrayAfterNSMutableArray *)arr{
for (int i = 10; i<20; i++) {
[arr addObject:[NSNumber numberWithInteger:i]];
}
}
我会给你一个例子。
让我们为字典定义我们自己的可变键,注意它可以被复制,它定义了 hash
和相等性的实现。
@interface MyKey : NSObject <NSCopying>
@property (nonatomic, copy, readwrite) NSString *keyData;
@end
@implementation MyKey
- (id)initWithDataNSString *)data {
self = [super init];
self.keyData = data;
return self;
}
- (id)copyWithZoneNSZone *)zone {
MyKey *key = (MyKey *) [[[self class] alloc] init];
key.keyData = self.keyData;
return key;
}
- (NSUInteger)hash {
return self.keyData.length;
}
- (BOOL)isEqualid)object {
if (![object isMemberOfClass:[self class]]) {
return NO;
}
MyKey *key = object;
return [self.keyData isEqualToString:key.keyData];
}
@end
现在让我们有一个简单的测试用例:
让我们定义一些键
MyKey *key1 = [[MyKey alloc] initWithData"key1"];
MyKey *key2 = [[MyKey alloc] initWithData"key2"];
MyKey *keyX = [[MyKey alloc] initWithData"XXX"];
还有一本简单的字典
NSDictionary *dictionary = @{key1: @"value1", key2: @"value2"};
让我们看看里面有什么
NSLog(@"%lu", (unsigned long) dictionary.count);
NSLog(@"%@", dictionary);
NSLog(@"%@", [dictionary objectForKey:key1]);
NSLog(@"%@", [dictionary objectForKey:key2]);
NSLog(@"%@", [dictionary objectForKey:keyX]);
给予(预期)
2
{
"<MyKey: 0x10010a8d0>" = value1;
"<MyKey: 0x100108e40>" = value2;
}
value1
value2
(null)
现在让我们改变一个键的值
[(MyKey *)[[dictionary allKeys] objectAtIndex:0] setKeyData"XXX"];
让我们再看看里面是什么
NSLog(@"%lu", (unsigned long) dictionary.count);
NSLog(@"%@", dictionary);
NSLog(@"%@", [dictionary objectForKey:key1]);
NSLog(@"%@", [dictionary objectForKey:key2]);
NSLog(@"%@", [dictionary objectForKey:keyX]);
给予
2 //expected
{
"<MyKey: 0x10010a8d0>" = (null); //huh, null value?
"<MyKey: 0x100108e40>" = value2;
}
(null) //huh?
value2
(null) //huh?
出了什么问题?
字典的内部结构是使用哈希表实现的。他们键的 hash
用于搜索值。一旦我们改变了key中的数据,hash值也会改变。因此,字典将无法找到为给定键存储的值。
关于ios - 集合中的可变对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20368173/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |