我可以创建要在NSMutableSet中使用的自定义对象吗?

时间:2021-09-21 18:53:28

I'm new to Objective-C, so please bear with me. I need to create an array (or a set) of name=value pairs with one condition that names can repeat in the set (thus I can't use NSMutableDictionary.) I found out that NSMutableSet seems to be what I need, but my dilemma now is how to add two NSString objects into it?

我是Objective-C的新手,所以请耐心等待。我需要创建一个名称=值对的数组(或一组),其中一个条件名称可以在集合中重复(因此我不能使用NSMutableDictionary。)我发现NSMutableSet似乎是我需要的,但我的困境现在是如何将两个NSString对象添加到其中?

4 个解决方案

#1


1  

You probably cannot use NSMutableSet either because sets do uniquing by hash. If you have two objects that have the same hash (like two NSStrings that have the same characters) then you will end up with just one string in the set as well.

您可能无法使用NSMutableSet,因为集合通过散列进行单独处理。如果你有两个具有相同哈希值的对象(比如两个具有相同字符的NSStrings),那么你最终只能在集合中使用一个字符串。

You probably would want to use an NSMutableArray where you put an object that has a key property and a value property that points to the actual object. Alternatively you can have your values in the NSArray by themselves and if you are looking for a certain one you can walk through the array and get the objects that have the matching key. Or more elegantly filter the array by key using an NSPredicate.

您可能希望使用NSMutableArray,其中放置一个具有key属性和指向实际对象的value属性的对象。或者,您可以自己在NSArray中获取值,如果要查找某个值,可以遍历数组并获取具有匹配键的对象。或者更优雅地使用NSPredicate按键过滤数组。

For how to filter arrays with predicates see here: http://www.cocoanetics.com/2010/03/filtering-fun-with-predicates/

有关如何使用谓词过滤数组,请参见:http://www.cocoanetics.com/2010/03/filtering-fun-with-predicates/

#2


1  

You can still use a dictionary, but for the values use an array or a set. This way you can map the same key to multiple different values.

您仍然可以使用字典,但对于值,请使用数组或集合。这样,您可以将相同的键映射到多个不同的值。

#3


1  

I wouldn't use a set, I think you should still use an NSMutableDictionary as at the root of your problem is a key-based lookup.

我不会使用一个集合,我认为你仍然应该使用NSMutableDictionary,因为你问题的根源是一个基于密钥的查找。

What you want to do is have an NSMutableArray as the 'value' and the name as the key. If the key has not yet been used, create an NSMutableArray inside the dictionary with the first value. If the key has already been used, just add the value to the NSMutableArray already inside the dictionary.

你想要做的是将NSMutableArray作为'value'并将名称作为键。如果尚未使用该键,请在字典中使用第一个值创建NSMutableArray。如果已经使用了密钥,只需将值添加到字典内已存在的NSMutableArray中。

Here is some sample code for a "setValueForKey" method:

以下是“setValueForKey”方法的一些示例代码:

NSMutableDictionary *dictionary = /* Some mutable dictionary stored as an instance variable or similar */
NSString *keyToAdd; id valueToAdd;

NSMutableArray *arrayForKey = [dictionary objectForKey:keyToAdd];

if (arrayForKey != nil) { // if the array has been created before earlier in the program
    [arrayForKey addObject:valueToAdd]; // then just add the value directly to the array inside the dictionary
} else { // otherwise create the mutable array using the key
    NSMutableArray *newMutableArray = [[NSMutableArray alloc] initWithCapacity:0];

    [dictionary setObject:newMutableArray forKey:keyToAdd];

    [newMutableArray addObject:valueToAdd];
}

To implement this logic, technically you could create a subclass of NSDictionary, but I think a custom wrapper object would be semantically better.

要实现这个逻辑,从技术上讲,您可以创建NSDictionary的子类,但我认为自定义包装器对象在语义上会更好。

#4


0  

You should go for a NSDictionary because it is hash mapped and faster. You can create a custom class having a property of type array. store all values there and use that object as value and put a key.

您应该选择NSDictionary,因为它是哈希映射的并且速度更快。您可以创建具有array类型属性的自定义类。在那里存储所有值并将该对象用作值并放置一个键。

#1


1  

You probably cannot use NSMutableSet either because sets do uniquing by hash. If you have two objects that have the same hash (like two NSStrings that have the same characters) then you will end up with just one string in the set as well.

您可能无法使用NSMutableSet,因为集合通过散列进行单独处理。如果你有两个具有相同哈希值的对象(比如两个具有相同字符的NSStrings),那么你最终只能在集合中使用一个字符串。

You probably would want to use an NSMutableArray where you put an object that has a key property and a value property that points to the actual object. Alternatively you can have your values in the NSArray by themselves and if you are looking for a certain one you can walk through the array and get the objects that have the matching key. Or more elegantly filter the array by key using an NSPredicate.

您可能希望使用NSMutableArray,其中放置一个具有key属性和指向实际对象的value属性的对象。或者,您可以自己在NSArray中获取值,如果要查找某个值,可以遍历数组并获取具有匹配键的对象。或者更优雅地使用NSPredicate按键过滤数组。

For how to filter arrays with predicates see here: http://www.cocoanetics.com/2010/03/filtering-fun-with-predicates/

有关如何使用谓词过滤数组,请参见:http://www.cocoanetics.com/2010/03/filtering-fun-with-predicates/

#2


1  

You can still use a dictionary, but for the values use an array or a set. This way you can map the same key to multiple different values.

您仍然可以使用字典,但对于值,请使用数组或集合。这样,您可以将相同的键映射到多个不同的值。

#3


1  

I wouldn't use a set, I think you should still use an NSMutableDictionary as at the root of your problem is a key-based lookup.

我不会使用一个集合,我认为你仍然应该使用NSMutableDictionary,因为你问题的根源是一个基于密钥的查找。

What you want to do is have an NSMutableArray as the 'value' and the name as the key. If the key has not yet been used, create an NSMutableArray inside the dictionary with the first value. If the key has already been used, just add the value to the NSMutableArray already inside the dictionary.

你想要做的是将NSMutableArray作为'value'并将名称作为键。如果尚未使用该键,请在字典中使用第一个值创建NSMutableArray。如果已经使用了密钥,只需将值添加到字典内已存在的NSMutableArray中。

Here is some sample code for a "setValueForKey" method:

以下是“setValueForKey”方法的一些示例代码:

NSMutableDictionary *dictionary = /* Some mutable dictionary stored as an instance variable or similar */
NSString *keyToAdd; id valueToAdd;

NSMutableArray *arrayForKey = [dictionary objectForKey:keyToAdd];

if (arrayForKey != nil) { // if the array has been created before earlier in the program
    [arrayForKey addObject:valueToAdd]; // then just add the value directly to the array inside the dictionary
} else { // otherwise create the mutable array using the key
    NSMutableArray *newMutableArray = [[NSMutableArray alloc] initWithCapacity:0];

    [dictionary setObject:newMutableArray forKey:keyToAdd];

    [newMutableArray addObject:valueToAdd];
}

To implement this logic, technically you could create a subclass of NSDictionary, but I think a custom wrapper object would be semantically better.

要实现这个逻辑,从技术上讲,您可以创建NSDictionary的子类,但我认为自定义包装器对象在语义上会更好。

#4


0  

You should go for a NSDictionary because it is hash mapped and faster. You can create a custom class having a property of type array. store all values there and use that object as value and put a key.

您应该选择NSDictionary,因为它是哈希映射的并且速度更快。您可以创建具有array类型属性的自定义类。在那里存储所有值并将该对象用作值并放置一个键。