Here are some snippets I coded and I would like some feedback on my way of handling this:
I have a utility class, as a singleton, that provides me with a method named randomColor
which returns a (UIColor*)
:
-(UIColor*)randomColor
{
float l_fRandomRedColor = [[MathUtility instance] randomFloatNumberBetweenNumber:0.0f AndNumber:1.0f];
float l_fRandomBlueColor = [[MathUtility instance] randomFloatNumberBetweenNumber:0.0f AndNumber:1.0f];
float l_fRandomGreenColor = [[MathUtility instance] randomFloatNumberBetweenNumber:0.0f AndNumber:1.0f];
return [UIColor colorWithRed:l_fRandomRedColor
green: l_fRandomGreenColor
blue: l_fRandomBlueColor
alpha: 255];
}
Now, I know the object return by this method is autoreleased
.
I store the returned value in another class and I would like to keep this value for a while, so I proceed like this:
[self setMpCurrentPieceColor:[[GraphicsUtility instance] randomColor]];
Which calls the following property:
- (void)setMpCurrentPieceColor:(UIColor*)_newColor
{
[_mpCurrentPieceColor release];
[_newColor retain];
// Make the new assignment.
_mpCurrentPieceColor = _newColor;
}
Question A)
My instance variable is released in the dealloc method. Is a the correct way to go?
Question B)
Now, let's imagine I have an array, like this:
UIColor* mBoardColors[WIDTH][HEIGHT];
I want to store the previous instance variable into the array:
[mBoardColors[l_iBoardXIndex][l_iBoardYIndex] release];
[_color retain];
mBoardColors[l_iBoardXIndex][l_iBoardYIndex] = _color;
Is it correct?
Question C)
What if I want to move a color from a cell to another (moving, not copying), is it correct to do it like that?
[mBoardColors[l_iBoardXIndex][l_iBoardYIndex] release];
mBoardColors[l_iBoardXIndex][l_iBoardYIndex] = mBoardColors[l_iBoardXIndex][l_iBoardYIndex - 1];
mBoardColors[l_iBoardXIndex][l_iBoardYIndex - 1] = nil;
Thank in advance for your precious comments and advices!