ios parse issue nstimer tutorial - iphone

I have followed the tutorial:
www.edumobile.org/iphone/iphone-programming-tutorials/a-simple-stopwatch-for-iphone
and I get 1 error and 1 warning, both on the same line 71
for (int i = [timeArray count] – 1; i >= 0; i–) {
error – a parse issue Expected )
warning – Unused entity issue Expression result unused
Any ideas what is wrong?

Change this,
for (int i = [timeArray count] – 1; i >= 0; i–) {
to,
for (int i = [timeArray count] – 1; i >= 0; i--) {
Compiler is saying that it is not able to parse the character '–'. If it is not able to recognize the for loop syntax and parse it, it will throw this error.

As ACB mentioned, the expression needs to be i-- instead of i-.
Just a couple of notes - Douglas Crawford actually recommends to avoid using -- and ++ in favor of doing i -= 1. While a smidgen verbose, there is no room for doubt over what it actually does versus something like
int example = --i + b;
may confuse some to the value of i after the end of the expression.
Also, as a minor optimization, you should put the size of the array in a local value as opposed to calling [timeArray count] every loop iteration
int timeArraySize = [timeArray count] - 1;
for (int i = timeArraySize; i >= 0; i -= 1) {
Hope that helps!

Related

Convert Java three part for loop to Swift

I'm having a great deal of difficulty translating the following for loop into Swft
for (int i = 0; i * denomAmount <= amount; i++ {
}
my attempt is
for i in stride(from: 0, to: amount, by: denom){
}
but obviously it amount is not the maximum value of i. So what is the best way to approach this?
Most likely either demonAmount or amount is being updated inside the loop. So I would use a while loop:
var i = 0
while i * demonAmount <= amount {
// the loop code
i += 1
}

can a constant pointer be changed during for iterations?

I've just started learning C++ with openCV and came across a weird thing, for me at least...
in this code, there's a for loop, in which there is an assignment to a "const uchar*" as a function of the index (j).
for(int j = 1; j < myImage.rows - 1; ++j)
{
const uchar* previous = myImage.ptr<uchar>(j - 1);
const uchar* current = myImage.ptr<uchar>(j );
const uchar* next = myImage.ptr<uchar>(j + 1);
uchar* output = Result.ptr<uchar>(j);
for(int i = nChannels; i < nChannels * (myImage.cols - 1); ++i)
{
*output++ = saturate_cast<uchar>(5 * current[i]
-current[i - nChannels] - current[i + nChannels] - previous[i] - next[i]);
}
}
As far as I know, constants can only be assigned once, during the construction, so what is going on in here?
I think I am missing a big part of the algorithm, and wish to understand its nature.
Please don't refer to the implementation itself for it is known to be non effective whatsoever. Also it's my first question posted to StackOverflow, so please consider ;)

Expression result unused

I got some codes and I'm trying to fix some compiling bugs:
StkFrames& PRCRev :: tick( StkFrames& frames, unsigned int channel )
{
#if defined(_STK_DEBUG_)
if ( channel >= frames.channels() - 1 ) {
errorString_ << "PRCRev::tick(): channel and StkFrames arguments are incompatible!";
handleError( StkError::FUNCTION_ARGUMENT );
}
#endif
StkFloat *samples = &frames[channel];
unsigned int hop = frames.channels();
for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
*samples = tick( *samples );
*samples++; <<<<<<<<<--------- Expression result unused.
*samples = lastFrame_[1];
}
return frames;
}
I don't understand what the codes is trying to do. The codes are huge and I fixed quite a few. But googling didn't work for this.
Any ideas?
First, you do an increment (the line which actually gives you warning).
*samples++;
And then you assign to that variable something else, which makes previous action unused.
*samples = lastFrame_[1];
I recommend you to read this code inside 'for' loop more carefully. It doesn't look very logical.

Return zero or positive number?

I was initially thinking that the code below would return 0, my question, is there a function that I can use to only receive zero/positive results here?
NSUInteger pruneSize = 5 - 20; // Returns: -15
Currently I am just checking the result myself, but was wondering if I was missing something simpler.
NSUInteger pruneSize = 5 - 20;
if(pruneSize >= 0) {
// Do zero/positive Stuff ...
}
pruneSize >= 0 is always true as pruneSize is unsigned. You should get a warning here. You need to change the type to NSInteger, that is the signed integer. If you want to clip the lower value to zero for a signed int then you can do this:
NSInteger pruneSize = 5 - 20; // signed int
pruneSize = pruneSize < 0 ? 0 : pruneSize;
You can use abs(pruneSize) which will return you positive or zero number in any case.
EDIT:
NSUInteger pruneSize = 5-20;
if(pruneSize < 0)
{
pruneSize = 0;
}
NSLog(#"%d",pruneSize);
Hope this helps you.
If you want your function to return always zero if your result is in negative(less than 0) then return zero or else return result
int n=0;
if(result > 0){ //positive
n = result
else
n = 0
return n
or use the abs method

iPhone - parsing nsdata to get multiple files stored there

I'm trying to find the new line and returns that are inside an nsdata object that I'm parsing . Here's some code:
uint8_t *arr = [receivedData bytes];
NSUInteger begin1 = 0;
NSUInteger end1 = len;
uint8_t *arr1 = (Byte *)malloc(sizeof(Byte)*((end1-begin1+1)));
int j = 0;
for (int i = begin1; i < end1; i++){
arr1[j] = arr[i];
j++;
if (arr[i] == 10) NSLog(#"---new line code---"); //edit: working - data was a problem
}
I just need to know when I hit a new line or return.
Thank You.
That certainly looks correct. Are you certain that the data you're parsing has newlines? Could it be a /r instead of a /n? If you can, try using the debugger and step through to see what the values actually are, and compare with what you expect them to be, to make sure they are correct.