0

I have an array (itemArray) that contains a number of objects. Each of these objects, which we'll refer to as item objects, has a property containing an item identifier that tells what type of item it is. We'll call that one IID.

Now, the user is going to be using the application to add instances of item to itemArray, and the user may add several identical instances (for example, 4 items, each with an IID of 3). Eventually, itemArray is going to contain arguably hundreds of instances of item, and those instances will be added in no particular order, and there could be several instances that are identical to other instances in the array (4 items with an IID of 3, 2 items with an IID of 6, etc etc).

I need to create an array (let's call it tempArray) that will be able to give a summary of the objects in the array based on the IID. I don't need to count the objects of each type in itemArray, I just need to add 1 instance of item to tempArray for each type of item in itemArray.

So, for example:

If my itemArray looks like this:

item.IID = 4
item.IID = 3
item.IID = 4
item.IID = 6
item.IID = 4
item.IID = 5
item.IID = 6
item.IID = 3`

Then I need tempArray too look like this:

item.IID = 4
item.IID = 3
item.IID = 6
item.IID = 5

Where tempArray just shows the variety of objects in itemArray based on the IID.

Thanks in advance!

i-developer
  • 441
  • 4
  • 12
JayB127
  • 73
  • 9
  • So you're not really trying to sort? And is IID item's only property? – Lyndsey Scott Jan 16 '15 at 14:17
  • I'm not trying to sort, really, so maybe the question title is a little bit misleading. I'm just trying to get tempArray to represent the variety of items in itemArray. The order that they go in isn't important. And item has other properties besides IID. – JayB127 Jan 16 '15 at 14:19

3 Answers3

2

If you can assume that an item is equal to another item based on their IIDs, I would implement the isEqual method checking the IIDs (returning YES if they're the same) and then use a NSSet to get the "filtered" list (once you have mentioned in your comment that the order isn't important to you). Like this:

@implementation YourItem

- (BOOL)isEqual:(id)object
{
    if (self != object) 
        return NO;

    if (![self.class isKindOfClass:[object class]]) 
        return NO;

    return self.IID == object.IID;
}

@end

As remembered by @jlehr, you need to override - (NSUInteger)hash too. You can do this by implemeting something like this:

- (NSUInteger)hash
{
    /* every property that will make it equals to another object */
    return [self.IID hash] ^ [self.name hash] /* ... ^ */;
}

(There is a great post written by Mike Ash explaining in full details Equality and Hashing)

and then...

NSSet *tempSet = [NSSet setWithArray:itemArray];

and if you want to convert it to a NSArray again:

NSArray *tempArray = [tempSet allObjects];
Gustavo Barbosa
  • 1,340
  • 1
  • 17
  • 27
1

If I understood you correctly, I would solve this using NSDictionary and would insert an item only if the key does not exist (item.iid is the key of the dictionary).

you can ask the dictionary if it has the object or it has the key, both works fine.

i-developer
  • 441
  • 4
  • 12
  • 1
    Additionally, the dictionary can return all the keys (`allKeys`) it contains and all the values (`allValues`) it contains as `NSArray` instances. – Andrew Monshizadeh Jan 16 '15 at 15:16
0

May be you can create NSSet from your array and then create NSArray from NSSet.

NSSet *tempSet = [NSSet setWithArray:yourArray]; NSArray *tempArray = [tempSet allObjects];

toofani
  • 1,650
  • 13
  • 16