Code will never be executed warning - objective-c

In my previous question, I asked why my application was terminating early. It turns out I'm getting this warning on one of the lines below and that may help me solve my problem.
Here's the code:
NSMutableArray *redValues = [NSMutableArray array];
NSUInteger redValuesLength = [redValues count];
float totalOne, diffForAverage;
int amount = 1;
__block int counter = 0;
NSInteger j;
GPUImageVideoCamera *videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];
videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
GPUImageAverageColor *averageColor = [[GPUImageAverageColor alloc] init];
[averageColor setColorAverageProcessingFinishedBlock:^(CGFloat redComponent, CGFloat greenComponent, CGFloat blueComponent, CGFloat alphaComponent, CMTime frameTime)
{
NSLog(#"%f", redComponent);
[redValues addObject:#(redComponent * 255)];
}];
[videoCamera addTarget:averageColor];
[videoCamera startCameraCapture];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 27 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[videoCamera stopCameraCapture];
});
totalOne = [redValues[25] floatValue];
float average = totalOne / amount;
for (j = (counter + 25); j < (redValuesLength - 25); j++)
{
diffForAverage = average - [redValues[j + 1] floatValue];
if (diffForAverage > -1 && diffForAverage < 1)
{
totalOne += [redValues[j + 1] floatValue];
amount++;
[arrayOne addObject:[NSNumber numberWithInt:(j - 25)]];
counter++;
}
else
{
if (arrayOneLength >= 15)
{
break;
counter++; // WARNING OCCURS HERE
}
else
{
[arrayOne removeAllObjects];
totalOne = [redValues[j + 1] floatValue];
counter++;
}
}
}
Why would the commented line above never be run? Is it a problem with my for loop, or my if statement, or something else entirely?

Because you have a break command just before it. The break command will immediately take you out of the for loop, so the next line won't be executed.

Related

Assigning to 'float' from incompatible type 'id'

The following error occurs on the two lines that are commented below:
Assigning to 'float' from incompatible type 'id'
NSMutableArray *redValues = [NSMutableArray array];
NSUInteger *redValuesLength = [redValues count];
NSMutableArray *arrayOne = [NSMutableArray array];
NSUInteger arrayOneLength = [arrayOne count];
__block int counter = 0;
int amount = 1;
float totalOne, diffForAverage;
NSInteger j;
totalOne = redValues[25]; // ERROR OCCURS HERE
float average = totalOne / amount;
for (j = (counter + 25); j < (redValuesLength - 25); j++)
{
diffForAverage = average - [redValues[j + 1] floatValue];
if (diffForAverage > -1 && diffForAverage < 1)
{
totalOne += [redValues[j + 1] floatValue];
amount++;
[arrayOne addObject:[NSNumber numberWithInt:(j - 25)]];
counter++;
}
else
{
if (arrayOneLength >= 15)
{
break;
counter++;
}
else
{
[arrayOne removeAllObjects];
totalOne = redValues[j + 1]; // ERROR OCCURS HERE
counter++;
}
}
}
Why is this error caused, and how can I fix it?
totalOne is float. And your array hold NSInteger. Change to that:
totalOne = [redValues[25] floatValue];

Invalid operands to binary expressions ("id" and "int")

I'm using the GPUImage framework to take the average red values of some camera input. I'm storing all of the average red values in an array, and I'm trying this:
NSMutableArray *redValues = [NSMutableArray array];
__block int counter = 0;
GPUImageVideoCamera *videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];
videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
GPUImageAverageColor *averageColor = [[GPUImageAverageColor alloc] init];
[averageColor setColorAverageProcessingFinishedBlock:^(CGFloat redComponent, CGFloat greenComponent, CGFloat blueComponent, CGFloat alphaComponent, CMTime frameTime)
{
NSLog(#"%f", redComponent);
[redValues addObject:#(redComponent)];
counter++;
}];
for (int i = 0; i < counter; i++)
{
redValues[i] *= 255; // errors occurs here
}
The error you get is because you are trying to multiply an NSNumber by an int. However, there is a deeper logical issue in that you are receiving redComponent as a value between 0 and 1. When you convert to an int, you truncate almost all values to 0 (at which point multiplying by 255 will not help much). Instead, you should inline the multiplication (and eliminate the second loop):
averageColor.colorAverageProcessingFinishedBlock = ^(CGFloat redComponent, CGFloat greenComponent, CGFloat blueComponent, CGFloat alphaComponent, CMTime frameTime) {
NSLog(#"%f", redComponent);
[redValues addObject:#(redComponent * 255)];
};

myArray count isn't functioning as expected

The arrayTwelveLEngth variable isn't working as expected. When I placed a breakpoint on the amount = 1; line above, I hovered over arrayTwelve, and found that it was empty with 0 elements. Immediately after, I then hovered about arrayTwelveLength, expecting to see 0, but instead it seems that the arrayTwelveLength had a value of 1876662112. I don't know how it got that value, and I need to solve that problem. What am I doing wrong?
NSMutableArray *redValues = [NSMutableArray array];
NSMutableArray *arrayTwelve = [NSMutableArray array];
__block int counter = 0;
__block NSInteger u;
NSUInteger redValuesLength = [redValues count];
__block int arrayTwelveLength = 0;
__block float diffForAverage, fps, averageTime, bloodSpeed;
float average;
__block int amount = 1;
__block float totalTwelve, totalThirteen;
__block NSUInteger totalNumberOfFramesInSmallArrays = 0;
__block NSUInteger totalNumberOfFramesNotInSmallArrays;
for (u = (counter + 24); u < (redValuesLength - 24); u++)
{
diffForAverage = average - [redValues[u + 1] floatValue];
float test = [redValues[u] floatValue];
arrayTwelveLength = [arrayTwelve count];
if (diffForAverage > -1 && diffForAverage < 1)
{
totalTwelve += [redValues[u + 1] floatValue];
amount++;
[arrayTwelve addObject:#(test)];
counter++;
}
else
{
if (arrayTwelveLength >= 8)
{
counter++;
break;
}
else
{
[arrayTwelve removeAllObjects];
totalTwelve = [redValues[u + 1] floatValue];
counter++;
amount = 1;
}
}
}
amount = 1; // I added a breakpoint here
totalThirteen = [redValues[u + 1] floatValue];
average = totalThirteen / amount;
if (counter == redValuesLength)
{
totalNumberOfFramesNotInSmallArrays = redValuesLength - totalNumberOfFramesInSmallArrays - 25 - (redValuesLength - counter);
fps = redValuesLength / 30;
averageTime = totalNumberOfFramesNotInSmallArrays / fps;
bloodSpeed = 3 / averageTime;
[_BloodSpeedValue setText:[NSString stringWithFormat:#"%f", bloodSpeed]];
}
if (arrayTwelveLength == NULL)
{
arrayTwelveLength = 0;
}
totalNumberOfFramesInSmallArrays += arrayTwelveLength;
You have problems with unsigned/signed types and with your data set the first for loop should not even enter, because your for loop index variable u (== 24) < (redValuesLength (== 0) - 24) but, because redValuesLength being Unsigned type it wraps around and you get:
(unsigned long)0 - (unsigned long)24 = -24 modulo ULONG_MAX + 1= 18446744073709551592
Also, you are not initialising average before usage.

Application terminating early because of GPUImageAverageColor block

I'm receiving the following message in the console after my application quits early because it is terminating with an uncaught exception of type NSException.
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 25 beyond bounds for empty array'
Here's my code:
NSMutableArray *redValues = [NSMutableArray array];
NSUInteger redValuesLength = [redValues count];
float totalOne, diffForAverage;
int amount = 1;
__block int counter = 0;
NSInteger j;
GPUImageVideoCamera *videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];
videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
GPUImageAverageColor *averageColor = [[GPUImageAverageColor alloc] init];
[averageColor setColorAverageProcessingFinishedBlock:^(CGFloat redComponent, CGFloat greenComponent, CGFloat blueComponent, CGFloat alphaComponent, CMTime frameTime)
{
NSLog(#"%f", redComponent);
[redValues addObject:#(redComponent * 255)];
}];
[videoCamera addTarget:averageColor];
[videoCamera startCameraCapture];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 27 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[videoCamera stopCameraCapture];
});
totalOne = [redValues[25] floatValue];
float average = totalOne / amount;
for (j = (counter + 25); j < (redValuesLength - 25); j++)
{
diffForAverage = average - [redValues[j + 1] floatValue];
if (diffForAverage > -1 && diffForAverage < 1)
{
totalOne += [redValues[j + 1] floatValue];
amount++;
[arrayOne addObject:[NSNumber numberWithInt:(j - 25)]];
counter++;
}
else
{
if (arrayOneLength >= 15)
{
break;
counter++;
}
else
{
[arrayOne removeAllObjects];
totalOne = [redValues[j + 1] floatValue];
counter++;
}
}
}
I understand that the error message I'm receiving means that my redValues array is empty, but I'm trying to visit and manipulate later elements. How can I solve this problem? I know that my GPUImageAverageColorBlock is printing red values correctly, but it seems that these values aren't being pushed into the array like I'm intending.

Doesn't draw my grid properly in objective C

I have a problem with counting my module. I am doing the following.
- (void)makeGrid:withData:(NSDictionary * )data
{
NSLog(#"aantal is: %d",[data count]);
int xStart = 0;
int yStart = 0;
int xCurrent = xStart;
int yCurrent = yStart;
int xStepSize = 165;
int yStepSize = 251;
int xCnt = 3 ;
int yCnt = [data count] % 3;
int cellCounter = 0;
UIView * gridContainerView = [[UIView alloc] init];
[keeperView addSubview:gridContainerView];
for (int y = 0; y < yCnt; y++) {
for (int x = 0; x < xCnt; x++) {
printf("xCurrent %d yCurrent %d \n", xCurrent, yCurrent);
NSString *url1 = #"player_imgUrl";
NSString *url2 = [NSString stringWithFormat:#"%i", x];
NSString *url3 = [url1 stringByAppendingString:url2];
NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[data objectForKey:url3]]];
UIImage* myImage = [[UIImage alloc] initWithData:imageData];
UIImageView * myView = [[UIImageView alloc] initWithImage:myImage];
CGRect rect = myView.frame;
rect.origin.x = xCurrent;
rect.origin.y = yCurrent;
myView.frame = rect;
myView.tag = cellCounter;
[gridContainerView addSubview:myView];
//just label stuff
UILabel * myLabel = [[UILabel alloc] init];
[gridContainerView addSubview:myLabel];
//--------------------------------
xCurrent += xStepSize;
cellCounter++;
}
xCurrent = xStart;
yCurrent += yStepSize;
}
CGRect repositionRect = gridContainerView.frame;
repositionRect.origin.y = 100;
gridContainerView.frame = repositionRect;
}
My NSLog says that in my data object are 16 values. And when I run it, it only shows 3 imageviews. Does anybody know what I am doing wrong?
Please help,
Kind regads.
After reading your code, the number of imageviews should be (xCnt * yCnt).
Your problem is here:
int yCnt = [data count] % 3;
the yCnt is 1 when your data is 16, that's why your result is 3 imageviews only.
to overcome this issue, you should do the following:
yCnt = (data.count / xCnt) + 1;
for (int y = 0 ; y < yCnt; y++)
{
for (int x = 0; x < xCnt; x++)
{
if ((y == (yCnt - 1)) && (x > (data.count % xCnt)))
{
break;
}
else {
// Your Grid code here
}
}
}
Hope This helps.
int yCnt = [data count] % 3;
gives 1 when [data count] == 16;