0

I have a method that returns an NSMutableArray of entities from my CoreData database. The entities look like this

@property (nonatomic, retain) NSNumber * iD;
@property (nonatomic, retain) NSString * number;
@property (nonatomic, retain) ManufacturerNumber *manufacturerNumber;

I need to create a unique array of manufacturerNumber entities based of the number NSString.

this is how my method that returns the current array with duplicates looks.

- (NSMutableArray *)readNumber
{
    NSManagedObjectContext *context = [self managedObjectContext];

    // Test listing all FailedBankInfos from the store
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"ManufacturerNumber" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];

    NSError *error;

    NSMutableArray *manufacturerNumbersDictionaryArray = [[NSMutableArray alloc] init];

    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
    for (ManufacturerNumber *manufacturerNumber in fetchedObjects) {

        [manufacturerNumbersDictionaryArray addObject:manufacturerNumber];

    }
    return manufacturerNumbersDictionaryArray;
}

This returns an NSMutableArray including duplicates, and I'd like to remove the duplicates.

Update to question

I have now decided to edit the array when I go to display the values in my UITableview, below I explain what the array contains etc.

I have a NSMutableArray that contains the coredata entities described above, I would like to know how to create a NSSet of unique values from the NSMutablerArray based from the entities cNumber attribute.

This is how I have created the NSMutableArray

tableViewMArray = [NSMutableArray arrayWithArray:[cardManufacturerNumber.cNumbers allObjects]];

As you can see cardManufacturerNumber is a coredata object with a one to many relationship with cNumbers.

cNumbers has 3 attributes

  • numString
  • numID
  • parentObj

I would like to know how to create a unique NSMutableArray based off cNumbers numString attribute.

The NSMutableArray should consisit of unique cNumbers coredata objects. not just numString strings as I need to know the other values.

halfer
  • 19,824
  • 17
  • 99
  • 186
HurkNburkS
  • 5,492
  • 19
  • 100
  • 183
  • Have you tried creating an `NSSet` from that mutable array? – andrewbuilder Jul 02 '14 at 23:15
  • No, I am trying to figure out how to do that now :) just reading up on NSSets I am having problems because of the coredata object – HurkNburkS Jul 02 '14 at 23:20
  • I am currently getting a warning Incompatible pointer types initializing 'NSSet *' with an expression of type 'NSString *' – HurkNburkS Jul 02 '14 at 23:36
  • If you want to eliminate duplicates from your data model, why does your data model allow duplicates? I'd think you would just check for existing objects in the store with numString when creating a new object. Mashing together an array without duplicates is a matter of using an NSDictionary's setValue:CNumber forKey:numString in a for loop up to tableViewMArray.count and then calling NSDictiorary's allValues. But that ignores issues with objects with the same numString potentially having different values for the other attributes... so your model should not allow it. – stevesliva Jul 03 '14 at 04:33
  • That is exactly the problem the other attributes do have different values.. It is an error in the data I am trying to overcome atm... a real pain in the butt – HurkNburkS Jul 03 '14 at 05:10

2 Answers2

0

Have you tried using NSMutableSet instead of NSMutableArray? If you properly implement - (NSUInteger)hash and - (BOOL)isEqual:(id)anObject in a way that makes logical sense on your ManufacturerNumber class this should give you the result you want (this is necessary since sets use these methods to ensure the uniqueness of each object. How you implement these depends on the internals of your ManufacturerNumber class). Or, if you care about order but want to maintain uniqueness, you could use NSMutableOrderedSet.

paulbramsen
  • 181
  • 1
  • 6
0

Unless I am missing the point, this should help for the first part of your question...

Regarding your method (NSMutableArray *)readNumber, try this instead...

- (NSSet *)readNumber
{
    NSSet *setReturned = nil;

    NSManagedObjectContext *context = [self managedObjectContext];

    // Test listing all FailedBankInfos from the store
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"ManufacturerNumber" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];

    NSError *error;

    NSMutableArray *manufacturerNumbersDictionaryArray = [[NSMutableArray alloc] init];

    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
    for (ManufacturerNumber *manufacturerNumber in fetchedObjects) {

        [manufacturerNumbersDictionaryArray addObject:manufacturerNumber];

    }
    setReturned = [NSSet setWithArray: manufacturerNumbersDictionaryArray];

    return setReturned;
}

This should return a discrete set of manufacturerNumber without duplicates.


For the second and updated part of your question, I'm struggling to understand exactly what you need, so forgive me if I am off the mark.

Have you attempted an NSFetchRequest with an NSPredicate, instead of the setting the variable tableViewMArray?

For example...

NSString *key = @"cNumbers";

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"cNumber"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K != %d", key, cardManufacturerNumber.cNumbers];
[fetchRequest setPredicate:predicate];
NSArray *array = [<<insert local managedObjectContext>> executeFetchRequest:fetchRequest error:nil];
NSSet *set = [NSSet setWithArray:array];

and then to make the array mutable...

(with thanks to this SO Q&A)

NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:[set allObjects]];
Community
  • 1
  • 1
andrewbuilder
  • 3,629
  • 2
  • 24
  • 46