rattletrap99
rattletrap99

Reputation: 1479

Color loop produces (null) instead of colors

@BJ Homer was good enough to post this code a couple of years ago. It was actually written to populate an array with colors, but I modified it slightly to populate a core data store for the purpose of driving a UICollectionView. It seems to be doing that, but all my colors are null (black).

Here's the code:

+(void) loadColorsIntoCD
{
    NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];

    int tag = 0;
    float INCREMENT = 0.0625;
    for (float hue = 0.0; hue < 1.0; hue += INCREMENT)
    {
        UIColor *color = [UIColor colorWithHue:hue
                                    saturation:1.0
                                    brightness:1.0
                                         alpha:1.0];

        [CatColor MR_createInContext:localContext];

        CatColor *thisColor;
        thisColor.color = color;
        thisColor.isTaken = NO;
        tag++;
        thisColor.idNumber = [NSNumber numberWithInt:tag];
        NSLog(@"ThisColor.idNumber is %@",thisColor.idNumber);
        NSLog(@"ThisColor.color is %@",thisColor.color);
        [localContext MR_saveToPersistentStoreAndWait];
    }
    NSLog(@"%i CatColors available", [CatColor MR_countOfEntities]);
}

Problem is, my NSLogs are reading:

ThisColor.idNumber is (null)

and

ThisColor.color is (null)

but then:

16 CatColors available

I'm really confused by this value which changes with each iteration through the loop, seeming to indicate that colors are being produced:

color   UIDeviceRGBColor *  0x8d83870   0x08d83870

Can someone please point out my mistake? I've been beating on this thing for the better part of a day.

Thanks for looking. All help much appreciated!

Upvotes: 0

Views: 73

Answers (1)

Marcelo
Marcelo

Reputation: 9944

You're not assigning the created entity to your local variable. You should do this:

CatColor *thisColor = [CatColor MR_createInContext:localContext];

Upvotes: 2

Related Questions