简体   繁体   中英

Setting elements of an array in Objective-C

My Objective-C is pretty rusty or non-existent. I'm trying to figure out how this code does what it does in order to modify it and having some trouble. This code basically draws lines at a pixel position on an ipad UIView (along the width of the iPad, like a density or column plot) based on the data it has. From the data, it calculates what values to assign and then another method draws the information.

In the .h file, this ivars are declared

ushort *mapBins;
int mapBinSize;
int mapBinMin;
int mapBinMax;

Then in the .m (commented out code is what I'm trying to do)

mapBins = 1024;
mapBins = malloc(mapBinSize * sizeof(ushort));
memset(mapBins, 0, mapBinSize * sizeof(ushort));


int pixelPos = 0;
// populate bins
for (int l=0; l<[locs count]; l++) // locs is a member variable that has positions to draw where there is valid data, everything else is blank
{
    int sLoc = [[locs objectAtIndex:l] intValue];
    pixelPos = (sLoc - self.startPos) / [self basesPerPixel];
    int index = pixelPos;
    if (index > mapBinMax) {
        continue;
    }
    if (index < 0 )
    {
        continue;
    }
        //        if ([m.type rangeOfString:@"A"].location == NSNotFound) {
//            mapBins[index] = 0;
//        }
//        else {
//            mapBins[index] = 1;
//            NSLog(@"1");
//        }
//        if ([m.type rangeOfString:@"T"].location == NSNotFound) {
//            mapBins[index] = 0;            
//        }
//        else {
//            mapBins[index] = 2;
//            NSLog(@"2");
//        }

    mapBins[index] += 1;
}
return YES;
}

So with their code, in the drawing code, it basically checks

 if (mapBins[posX] == 0) {
        [blank setStroke];
    }
 else {
      [YELLOW_COLOR setStroke];
 }

for each pixel. So now I want to change the colors based on the information I have from the data. Like in the commented out code, it shows a part of what I'm trying to do.

What I don't understand is, I set the mapBin[index] to something other than 0. I see that by the NSLog(@"1") statement on the console. However, if I log the output at the end of the loop by doing: NSLog(@"%i", mapBins[posX]); I get 0 as the output. And when I try drawing it, I get a blankStroke instead of a colored stroke since the value is 0.

Is something going on that I do not understand in C? What I gather is they are doing this (could be wrong though):

  • set the mapBinSize to the size of one screen length or 1024 pixels
  • create memory for that array?
  • then based on the data, if there is a value at that bin, add a number so it can draw a value at that region later on.

您注释掉的逻辑将始终为0或2。无论您从比较中设置为@“ A”的结果如何,以下if / else都会无条件地覆盖。

From context, I'm guessing that this:

mapBins = 1024;

...should be this:

mapBinSize = 1024;

There's also no mention of snpMapBins in the code you've posted and the code you're talking about is commented out.

It looks like the code you've posted isn't representative of what you are actually running. Can you please post the actual code?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM